스프링노트

[PHP] XML -> JSON 변환 본문

DEVELOPMENT/PHP

[PHP] XML -> JSON 변환

RAYZIE 2015. 4. 1. 23:37
<?php
/*
    Description : The following class converts XML data into JSON format
    Author: Dnyanesh Pawar,
    Copyright: Dnyanesh Pawar (http://www.techrecite.com)
    Link: http://www.techrecite.com
    See the GNU General Public License for more details: http://www.creativecommons.org/
*/
 
class XmlToJsonConverter {
    public function ParseXML ($url) {
        $fileContents= file_get_contents($url);
        // Remove tabs, newline, whitespaces in the content array
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        $myXml = simplexml_load_string($fileContents);
        $json = json_encode($myXml);
        return $json;
    }
}
//Path of the XML file
$url= 'test.xml';
 
//Create object of the class
$jsonObj = new XmlToJsonConverter();
 
//Pass the xml document to the class function
$myjson = $jsonObj->ParseXMl($url);
print_r ($myjson);
?>


test.xml 부분에 자기 파일주소나 URL 넣으시면 됩니다.


출처 : http://www.techrecite.com/xml-to-json-data-parser-converter/

'DEVELOPMENT > PHP' 카테고리의 다른 글

[PHP] Directory Listing (디렉토리 리스팅)  (0) 2013.10.24
[APM_SETUP] 설치하기  (0) 2013.05.01
PHP 간단한 로그인 인증  (0) 2013.04.18