분류 php

Convert XML to JSON in PHP

컨텐츠 정보

  • 조회 671 (작성일 )

본문

function XMLtoJSON($xml) {

  $xml = file_get_contents($xml);    // gets XML content from file

  $xml = str_replace(array("\n", "\r", "\t"), '', $xml);    // removes newlines, returns and tabs

 

  // replace double quotes with single quotes, to ensure the simple XML function can parse the XML

  $xml = trim(str_replace('"', "'", $xml));

  $simpleXml = simplexml_load_string($xml);

 

  return stripslashes(json_encode($simpleXml));    // returns a string with JSON object

php