본문 바로가기

DEVELOPMENT/PHP

[PHP] XML -> JSON 변환

<?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);
?>



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

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

[PHP] 간단한 로그인 인증  (0) 2014.03.04
[PHP] Directory Listing (디렉토리 리스팅)  (0) 2014.03.04