SPRING :: NOTE

[PHP] XML -> JSON 변환 본문

Development Language/WEB

[PHP] XML -> JSON 변환

RAYZIE 2016. 6. 8. 18:51
반응형
/*
    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/


반응형
Comments