With the continuous development of Internet technology, data processing has become an indispensable part of Internet application development. XML and JSON have also become the two most commonly used data exchange formats. However, during the actual application development process, we found that PHP supports parsing XML format by default, but its support for JSON format is relatively limited. Therefore, this article will introduce how to use PHP to write functions to convert XML format data into JSON format data.
1. Overview of requirements
In the actual application development process, we need to convert XML format data into JSON format data to achieve data sharing, exchange and other functions. Specifically, we need to parse the original XML data, convert it into a PHP associative array, and then convert the array into JSON format data and output it to the client.
2. Solution
In order to achieve the above requirements, we need to implement the following three steps:
1. Parse XML data
Use PHP’s built-in The function simplexml_load_string() can convert XML format data into PHP objects. The syntax of this function is as follows:
mixed simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )
Among them, the $data parameter represents the XML string that needs to be parsed, and the return value is the parsed object. When parsing XML data, we need to ensure the correctness of the data. Therefore, the simplexml_load_string() function can be encapsulated into a check_xml() function to verify whether the XML data is legal.
function check_xml($xml_str) { $obj = @simplexml_load_string($xml_str); if ($obj === false) { return false; } return true; }
2. Convert XML data into PHP array
After parsing the XML format data, we need to convert it into a PHP array. In order to convert an object into an array, you can use the objectToArray() function in PHP.
function objectToArray($object) { $arr = is_object($object) ? get_object_vars($object) : $object; if (is_array($arr)) { return array_map(__FUNCTION__, $arr); } else { return $arr; } }
Combined with the previous check_xml() function, the convert_xml_to_array() function can be encapsulated as follows:
function convert_xml_to_array($xml_str) { $obj = @simplexml_load_string($xml_str); if ($obj === false) { return false; } $arr = objectToArray($obj); return $arr; }
3. Convert the PHP array into JSON format data
The last step is Convert PHP array to JSON format data. In PHP, you can use the json_encode() function to easily convert PHP arrays into JSON format data. The syntax is as follows:
string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
The $value parameter represents the PHP array that needs to be converted into JSON format data, and the return value is a string in JSON format. In order to facilitate encapsulation, we can define the convert_array_to_json() function as follows:
function convert_array_to_json($arr) { $json = json_encode($arr, JSON_UNESCAPED_UNICODE); return $json; }
3. Complete code
In summary, we can encapsulate the above three steps into a complete function convert_xml_to_json (), the code is as follows:
function convert_xml_to_json($xml_str) { $arr = convert_xml_to_array($xml_str); if ($arr === false) { return false; } $json = convert_array_to_json($arr); return $json; } function check_xml($xml_str) { $obj = @simplexml_load_string($xml_str); if ($obj === false) { return false; } return true; } function convert_xml_to_array($xml_str) { $obj = @simplexml_load_string($xml_str); if ($obj === false) { return false; } $arr = objectToArray($obj); return $arr; } function convert_array_to_json($arr) { $json = json_encode($arr, JSON_UNESCAPED_UNICODE); return $json; } function objectToArray($object) { $arr = is_object($object) ? get_object_vars($object) : $object; if (is_array($arr)) { return array_map(__FUNCTION__, $arr); } else { return $arr; } }
4. Usage examples
The usage examples are as follows:
$xml_str = <<<XML <?xml version="1.0" encoding="UTF-8"?> <xml> <name>张三</name> <age>20</age> <gender>男</gender> <education> <degree>学士</degree> <school>清华大学</school> </education> <education> <degree>硕士</degree> <school>北京大学</school> </education> </xml> XML; if (check_xml($xml_str)) { $json = convert_xml_to_json($xml_str); echo "JSON: {$json}"; } else { echo "XML数据不合法"; }
The running results are as follows:
JSON: {"name":"u5f20u4e09","age":"20","gender":"u7537","education":[{"degree":"u5b66u58eb","school":"u6e05u534eu5927u5b66"},{"degree":"u7855u58eb","school":"u5317u4eacu5927u5b66"}]}
5. Summary
In this article, we introduce how to use PHP to write functions to convert XML format data into JSON format data. By using simplexml_load_string() to parse XML data, objectToArray() to convert PHP objects into PHP arrays, and json_encode() to convert PHP arrays into JSON format data, we can quickly and easily implement data format conversion, and can develop practical applications be applied to improve the scalability and ease of use of the program.
The above is the detailed content of PHP application: Use functions to convert XML to JSON format. For more information, please follow other related articles on the PHP Chinese website!