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 is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Convert XML to JSON in PHP

Last accessed pages

  1. Display image file in Canvas, Check its type and size before Upload (642)
  2. Bubble Hit (107)
  3. addChild and removeChild (3398)
  4. Svetlograd Zuma (1161)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (28165)

Top accessed pages

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