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, ∼2928🔥, 0💬
Popular Posts:
How To Loop through an Array without Using "foreach" in PHP? PHP offers the following functions to a...
How to use "json-to-xml" Azure API Policy Statement? The "json-to-xml" Policy Statement allows you t...
How to install "C++/CLI Support" component in Visual Studio? I need to build my Visual Studio C++/CL...
How to create a new API on the Publisher Dashboard of an Azure API Management Service? If you are ne...
How to use the RSS Online Validator at w3.org? You can follow this tutorial to learn how to use the ...