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, ∼2834🔥, 0💬
Popular Posts:
What Is Azure API Management Service? Azure API Management as a turnkey solution for publishing APIs...
How to detect errors occurred in the json_decode() call? You can use the following two functions to ...
How To Read the Entire File into a Single String in PHP? If you have a file, and you want to read th...
How to detect errors occurred in the json_decode() call? You can use the following two functions to ...
How to use the "rewrite-uri" Policy Statement for an Azure API service operation? The "rewrite-uri" ...