>  기사  >  백엔드 개발  >  PHP XML을 JSON으로

PHP XML을 JSON으로

WBOY
WBOY원래의
2024-08-29 13:00:46419검색

PHP에서 XML을 JSON으로 변환하기 위해 json_encode 함수라는 함수가 있는데, 이는 PHP에 내장된 함수이며 XML을 JSON으로 변환하는 절차는 먼저 다음을 사용하여 XML 파일의 내용을 가져옵니다. XML 파일의 URL이 매개변수로 전달되는 _file_get_contents()_ 함수, 그런 다음 리턴, 탭 및 개행 문자가 제거되고 큰따옴표가 작은따옴표로 대체된 다음 후행 및 선행 공백이 대체됩니다. XML이 간단한 XML 함수로 제대로 구문 분석되었는지 확인하기 위해 잘린 다음 json_encode 함수를 사용하여 최종 변환이 이루어집니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

PHP에서 Zlib 모듈을 선언하는 구문:

json_encode(URL_to_the_XML_file)

URL_to_the_XML_file은 JSON으로 변환할 XML 파일의 URL입니다.

PHP에서 XML을 JSON으로 변환하는 단계

  • XML 파일의 URL을 매개변수로 전달하는 file_get_contents() 함수를 사용하여 XML 파일 내용을 가져옵니다.
  • 탭, 리턴 및 줄바꿈을 제거합니다.
  • 작은따옴표가 큰따옴표를 대체합니다.
  • 간단한 XML 함수를 통해 XML이 올바르게 구문 분석되도록 후행 및 선행 공백이 잘립니다.
  • Simplexml_load_string() 함수는 XML 파일의 내용을 로드하기 위해 호출됩니다.
  • XML을 JSON으로 최종 변환하는 작업은 json_encode() 함수를 호출하여 수행됩니다.

PHP XML을 JSON으로 변환하는 예

다음은 PHP XML을 JSON으로 변환하는 예입니다.

예시 #1

XML 파일의 내용을 JSON으로 변환하기 위해 json_encode 함수에 대한 매개변수로 XML 파일의 URL을 제공하는 XML에서 JSON으로의 변환을 보여주는 PHP 프로그램입니다.

코드:

<html>
<body>
<?php
class XmlToJson {
public function Parse ("C://Users/admin/Desktop/check.xml") {
# Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter
$filepath= file_get_contents("C://Users/admin/Desktop/check.xml");
# Removing the tabs, returns and the newlines
$filechange = str_replace(array("\n", "\r", "\t"), '', $filepath);
# The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
$filetrim = trim(str_replace('"', "'", $filechange));
# The simplexml_load_string() function is called to load the contents of the XML file.
$resultxml = simplexml_load_string($filetrim);
# The final conversion of XML to JSON is done by calling the json_encode() function.
$resultjson = json_encode($resultxml);
return $resultjson;
}
}
?>
</body>
</html>

출력:

PHP XML을 JSON으로

위 프로그램에서는 XML 파일의 URL이 매개변수로 전달되는 file_get_contents() 함수를 사용하여 XML 파일 내용을 가져옵니다. 그런 다음 탭, 리턴 및 개행 문자가 제거됩니다. 그런 다음 큰따옴표가 작은따옴표로 대체됩니다. 그런 다음 후행 및 선행 공백이 잘려서 간단한 XML 함수로 XML이 제대로 구문 분석되는지 확인합니다. 그런 다음 simplexml_load_string() 함수가 호출되어 XML 파일의 내용을 로드합니다. 그런 다음 json_encode() 함수를 호출하여 XML을 JSON으로 최종 변환합니다.

예시 #2

XML 파일의 내용을 JSON으로 변환하기 위해 json_encode 함수에 대한 매개변수로 XML 파일의 URL을 제공하는 XML에서 JSON으로의 변환을 보여주는 PHP 프로그램입니다.

코드:

<html>
<body>
<?php
class XmlToJson {
public function Parse ("C://Users/admin/Desktop/test.xml") {
# Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter
$filepath= file_get_contents("C://Users/admin/Desktop/test.xml");
# Removing the tabs, returns and the newlines
$filechange = str_replace(array("\n", "\r", "\t"), '', $filepath);
# The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
$filetrim = trim(str_replace('"', "'", $filechange));
# The simplexml_load_string() function is called to load the contents of the XML file.
$resultxml = simplexml_load_string($filetrim);
# The final conversion of XML to JSON is done by calling the json_encode() function.
$resultjson = json_encode($resultxml);
return $resultjson;
}
}
?>
</body>
</html>

출력:

PHP XML을 JSON으로

위 프로그램에서는 XML 파일의 URL이 매개변수로 전달되는 file_get_contents() 함수를 사용하여 XML 파일 내용을 가져옵니다. 그런 다음 탭, 리턴 및 개행 문자가 제거됩니다. 그런 다음 큰따옴표가 작은따옴표로 대체됩니다. 그런 다음 후행 및 선행 공백이 잘려서 간단한 XML 함수로 XML이 제대로 구문 분석되는지 확인합니다. 그런 다음 simplexml_load_string() 함수가 호출되어 XML 파일의 내용을 로드합니다. 그런 다음 json_encode() 함수를 호출하여 XML을 JSON으로 최종 변환합니다.

예시 #3

XML 파일의 내용을 JSON으로 변환하기 위해 json_encode 함수에 대한 매개변수로 XML 파일의 URL을 제공하는 XML에서 JSON으로의 변환을 보여주는 PHP 프로그램입니다.

코드:

<html>
<body>
<?php
class XmlToJson {
public function Parse ("C://Users/admin/Desktop/file.xml") {
# Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter
$filepath= file_get_contents("C://Users/admin/Desktop/file.xml");
# Removing the tabs, returns and the newlines
$filechange = str_replace(array("\n", "\r", "\t"), '', $filepath);
# The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
$filetrim = trim(str_replace('"', "'", $filechange));
# The simplexml_load_string() function is called to load the contents of the XML file.
$resultxml = simplexml_load_string($filetrim);
# The final conversion of XML to JSON is done by calling the json_encode() function.
$resultjson = json_encode($resultxml);
return $resultjson;
}
}
?>
</body>
</html>

출력:

PHP XML을 JSON으로

위 프로그램에서는 XML 파일의 URL이 매개변수로 전달되는 file_get_contents() 함수를 사용하여 XML 파일 내용을 가져옵니다. 그런 다음 탭, 리턴 및 개행 문자가 제거됩니다. 그런 다음 큰따옴표가 작은따옴표로 대체됩니다. 그런 다음 후행 및 선행 공백이 잘려서 간단한 XML 함수로 XML이 제대로 구문 분석되는지 확인합니다. 그런 다음 simplexml_load_string() 함수가 호출되어 XML 파일의 내용을 로드합니다. 그런 다음 json_encode() 함수를 호출하여 XML을 JSON으로 최종 변환합니다.

위 내용은 PHP XML을 JSON으로의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 객체 직렬화다음 기사:PHP 객체 직렬화