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.

Code of XMLtoJSON()

// 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/"}]}

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
}
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
alert(content);
Click on the "echo" correct instruction.
echo "CoursesWeb.net" echo "CoursesWeb.net"; echo ""CoursesWeb.net";
echo "Address URL: http://CoursesWeb.net";
Convert XML to JSON in PHP

Last accessed pages

  1. Inheritance - Parent class and Child class (1109)
  2. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (27235)
  3. Get the value of multiple selected checkboxes with same name (4347)
  4. Add, Change, and Remove Attributes with jQuery (21290)
  5. Common PHP Errors and Solutions (4654)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (43787)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (33177)
  3. Read Excel file data in PHP - PhpExcelReader (33092)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (29923)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (27235)