Friday, 26 June 2015

converts XML content to JSON

 
The PHP function presented in this page can be used to convert XML content to JSON string. The XMLtoJSON() function receives the URL address of the XML file. Returns a string with the JSON object.

 • This function cannot be used with XML content with Namespace.
 
 
// converts XML content to JSON
// receives the URL address of the XML file. Returns a string with the JSON object
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
}
- Just add the XMLtoJSON() function in you PHP file, and call it passing the URL address of the XML file.

- If you want to convert XML content stored into a string, delete or comment this line of code:
$xml = file_get_contents($xml);
And call the XMLtoJSON() function passing the string with the XML content.
$strxml = 'xml content';
$strjson = XMLtoJSON($strxml);
• This function can also be used to easily convert XML content to Array. Just call the function to get the string with JSON object, then apply the json_decode() to convert the JSON string to Array.
$strjson = XMLtoJSON($xml);
$arrjson = json_decode($strjson, true);

Examples usage XMLtoJSON()

1. XML content stored into a file named "test1.xml".
<?xml version="1.0" encoding="UTF-8"?>
<websites>
  <site>
    <title>Web Programming Courses</title>
    <url>http://coursesweb.net/</url>
  </site>
  <site>
    <title>Courses Games Anime</title>
    <url>http://www.marplo.net/</url>
  </site>
</websites>
PHP code:
<?php
// converts XML content to JSON
// receives the URL address of the XML file. Returns a string with the JSON object
function XMLtoJSON($xml) {
  $xml_cnt = file_get_contents($xml);    // gets XML content from file
  $xml_cnt = str_replace(array("\n", "\r", "\t"), '', $xml_cnt);    // removes newlines, returns and tabs

  // replace double quotes with single quotes, to ensure the simple XML function can parse the XML
  $xml_cnt = trim(str_replace('"', "'", $xml_cnt));
  $simpleXml = simplexml_load_string($xml_cnt);

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

echo XMLtoJSON('test1.xml');
Results:
{"site":[{"title":"Web Programming Courses","url":"http://coursesweb.net/"},{"title":"Courses Games Anime","url":"http://www.marplo.net/"}]}
2. XML content with IDs in tags, and added into a string:
<?php
// converts XML content to JSON
// receives a string with the XML content. Returns a string with the JSON object
function XMLtoJSON($xml) {
  $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
}

$strxml = '<?xml version="1.0" encoding="UTF-8"?>
<websites>
  <site id="1" pr="5">
    <title>Web Programming Courses</title>
    <url>http://coursesweb.net/</url>
  </site>
  <site id="2" pr="4">
    <title>Courses Games Anime</title>
    <url>http://www.marplo.net/</url>
  </site>
</websites>';

echo XMLtoJSON($strxml);
Results:
{"site":[{"@attributes":{"id":"1","pr":"5"},"title":"Web Programming Courses","url":"http://coursesweb.net/"},{"@attributes":{"id":"2","pr":"4"},"title":"Courses Games Anime","url":"http://www.marplo.net/"}]}

0 comments:

Post a Comment