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 renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Convert XML to JSON in PHP

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (54304)
  2. The Rise Of Atlantis (7932)
  3. Zuma Deluxe (32813)
  4. Butterfly Kyodai (24554)
  5. SHA512 Encrypt hash in JavaScript (6645)

Popular pages this month

  1. Bubbles3 (2864)
  2. Butterfly Kyodai (2474)
  3. Zuma Deluxe (1770)
  4. Qwop (1183)
  5. Backgammon (1081)