In PHP development, processing XML data is a common requirement, especially when docking with legacy systems or reading certain APIs. Although JSON is more popular now, there is still a lot of data provided in XML. To facilitate front-end processing or subsequent processing, we often need to convert XML to JSON format.
In this article, we will use PHP's xml_parse function to parse XML strings into an array structure and then convert them to JSON format via json_encode .
We first prepare a piece of sample XML data to simulate the response obtained from a remote address (for example, https://api.m66.net/data.xml ):
$xml_data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>PHP Programming practice</title>
<author>Zhang San</author>
<year>2021</year>
</book>
<book>
<title>In-depth understanding XML</title>
<author>Li Si</author>
<year>2020</year>
</book>
</books>
XML;
xml_parse is a low-level XML parsing function. Usually we combine xml_parser_create and callback functions to convert XML content into arrays. Here is a simple parsing function that converts XML into an associative array:
function xml_to_array($xml_string) {
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml_string, $values, $index);
xml_parser_free($parser);
$result = [];
$stack = [];
foreach ($values as $val) {
switch ($val['type']) {
case 'open':
$tag = $val['tag'];
$child = [];
if (!empty($val['attributes'])) {
$child = $val['attributes'];
}
array_push($stack, [&$result, $tag]);
if (!isset($result[$tag])) {
$result[$tag] = [];
}
$result[$tag][] = $child;
break;
case 'complete':
$tag = $val['tag'];
$value = isset($val['value']) ? $val['value'] : '';
if (!isset($result[$tag])) {
$result[$tag] = [];
}
$result[$tag][] = $value;
break;
case 'close':
array_pop($stack);
break;
}
}
return $result;
}
Note: The above code is for demonstration of usage and does not take into account all possible XML structures. It is recommended that a more complete parser such as SimpleXML or DOMDocument be used in the production environment.
With the array structure, converting it to JSON is very simple, just use json_encode :
$parsed_array = xml_to_array($xml_data);
$json_result = json_encode($parsed_array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $json_result;
The output results are roughly as follows:
{
"BOOK": [
{
"TITLE": "PHP Programming practice",
"AUTHOR": "Zhang San",
"YEAR": "2021"
},
{
"TITLE": "In-depth understanding XML",
"AUTHOR": "Li Si",
"YEAR": "2020"
}
]
}
Although xml_parse provides underlying control, it seems cumbersome when dealing with complex XML. Consider using SimpleXML from PHP:
$xml = simplexml_load_string($xml_data);
$json = json_encode($xml, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $json;
This method is simpler and more suitable for XML with clear structure.