Tools, FAQ, Tutorials:
Convert XML to JSON with PHP
How to convert an XML document to a JSON text string with PHP language?
✍: FYIcenter.com
Currently, there is no built-in function or any standard extension that you can use to convert an XML document to a JSON text string.
But you can use the following PHP example, xml_to_json_converter.php, to convert an XML document to a JSON text string:
<?php # xml_to_json_converter.php # Copyright (c) FYIcenter.com $dom = new DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->load($argv[1]) or die("Unable to open json file!"); $root = $dom->documentElement; $tag = $root->tagName; $obj = new stdClass(); $obj->{$tag} = buildJSON($root); print(json_encode($obj,JSON_PRETTY_PRINT)); function buildJSON($element) { $val = null; $att = buildAttributes($element); $txt = buildText($element); $sub = buildElements($element); if ($att!=null) { if ($val==null) $val = new stdClass(); foreach ($att as $k => $v) { $val->{$k} = $v; } } if ($sub!=null) { if ($val==null) $val = new stdClass(); foreach ($sub as $k => $v) { $val->{$k} = $v; } } if ($txt!=null) { if (($val==null) && is_scalar($txt)) { $val = $txt; } else { if ($val==null) $val = new stdClass(); $val->{"#text"} = $txt; } } return $val; } function buildAttributes($element) { $val = null; if ($element->hasAttributes()) { $val = new stdClass(); foreach ($element->attributes as $att) { $v = $att->value; if ($v=="") $v = null; $val->{"@".$att->name} = $v; } } return $val; } function buildText($element) { $arr = array(); if ($element->hasChildNodes()) { foreach ($element->childNodes as $node) { if ($node->nodeType==XML_TEXT_NODE || $node->nodeType==XML_CDATA_SECTION_NODE) { array_push($arr,$node->nodeValue); } } } if (count($arr)==1) return $arr[0]; else if (count($arr)>1) return $arr; else return null; } function buildElements($element) { $val = new stdClass(); if ($element->hasChildNodes()) { foreach ($element->childNodes as $node) { if ($node->nodeType==XML_ELEMENT_NODE) { if (!property_exists($val,$node->nodeName)) { $val->{$node->nodeName} = buildJSON($node); } else { $cur = $val->{$node->nodeName}; if (is_array($cur)) { array_push($cur,buildJSON($node)); } else { $val->{$node->nodeName} = array($cur,buildJSON($node)); } } } } } if (count((array)$val)>0) return $val; else return null; } ?>
To try the above PHP example, you can enter the following XML file, xml_sample.xml:
<profile id="10001"> <name>John<mid>M</mid>Smith</name> <age>25</age> <address> <street>21 2nd Street</street> <city>New York</city> </address> <phone type="home">212 555-1234</phone> <phone type="office">646 555-4567</phone> <children/> </profile>
Then run the above PHP example through the PHP engine with the XML file provided in the command line:
>\fyicenter\php\php.exe xml_to_json_converter.php xml_sample.xml { "profile": { "@id": "10001", "name": { "mid": "M", "#text": [ "John", "Smith" ] }, "age": "25", "address": { "street": "21 2nd Street", "city": "New York" }, "phone": [ { "@type": "home", "#text": "212 555-1234" }, { "@type": "office", "#text": "646 555-4567" } ], "children": null } }
⇒ XML to JSON Conversion at fyicenter.com
2017-08-17, 2337🔥, 0💬
Popular Posts:
How to reinstall npm with a node version manager? I am getting permission errors with the current ve...
Where to get the detailed description of the JSON.stringify() Function in JavaScript? Here is the de...
How To Use an Array as a Queue in PHP? A queue is a simple data structure that manages data elements...
Where to find tutorials on RSS specifications? I want to learn it to describe my API services. Here ...
What properties and functions are supported on requests.models.Response objects? "requests" module s...