Php-mysql Course

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>https://coursesweb.net/</url>
  </site>
  <site>
    <title>Courses Games Anime</title>
    <url>https://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":"https://coursesweb.net/"},{"title":"Courses Games Anime","url":"https://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>https://coursesweb.net/</url>
  </site>
  <site id="2" pr="4">
    <title>Courses Games Anime</title>
    <url>https://marplo.net/</url>
  </site>
</websites>';

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

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Convert XML to JSON in PHP

Last accessed pages

  1. Download PHP-MySQL resources (1156)
  2. AJAX and XML (2351)
  3. PHP PDO - Introduction and Connecting to Databases (8631)
  4. Laravel Basic Architecture (1280)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141762)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (486)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (73)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)