Home >Backend Development >PHP Problem >How to convert php json to xml format

How to convert php json to xml format

PHPz
PHPzOriginal
2023-03-31 09:06:27965browse

PHP is a scripting language widely used in web development. Among them, JSON and XML formats are widely used in Web applications. Converting JSON format to XML format in PHP is also a very useful skill. This article will introduce how to convert JSON to XML in PHP.

1. Introduction to JSON and XML formats

JSON is the abbreviation of JavaScript Object Notation. It is a lightweight data exchange format commonly used to transmit data over the network. JSON is a data structure composed of key-value pairs, which is easy to read and write. It supports multiple programming languages ​​for parsing and generation. It is simple and intuitive, has a small amount of data, and is easy to parse.

XML is the abbreviation of Extensible Markup Language, a markup language similar to HTML that can be used to store and transmit data. XML data contains start tags, end tags and the data between them. XML is an extensible format that supports user-defined tags and is therefore more flexible, but its parsing efficiency is relatively low and the amount of data is large.

2. Method of converting JSON into XML

In PHP, to convert JSON into XML, you can use the SimpleXMLElement and json_decode functions. SimpleXMLElement is a class built into PHP that is used to create and manipulate XML document objects. The json_decode function is used to decode JSON format data into PHP arrays or objects.

The following is a sample code:

$json_str = '{
      "name":"John Smith",
      "age":32,
      "email":"johnsmith@example.com"
    }';

    $json_obj = json_decode($json_str); // 将json字符串解析为对象
    $xml_obj = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><data></data>'); // 创建XML对象

    function add_node($obj, $parent)
    {
        foreach ($obj as $key => $value) {
            $node = $parent->addChild(is_numeric($key)? "item":"$key"); // 创建XML元素,并设置元素名
            if(is_object($value) || is_array($value)) { // 判断值类型,如果是数组或对象类型,递归创建XML元素
                add_node($value, $node);
            } else {
                $node->addChild("value",$value); // 创建值元素
            }
        }
    }

    add_node($json_obj, $xml_obj); // 将json节点转化为XML元素

    $xml_str = $xml_obj->asXML(); // 将XML对象转化为XML字符串

    echo $xml_str;

In the code, a $json_str string is first defined, which contains a JSON data object. Then use the json_decode function to convert $json_str into a JSON object $json_obj. Next, create the XML object $xml_obj through the new SimpleXMLElement function.

In order to convert the JSON object into XML, the add_node function below uses a recursive method to convert each JSON value node into an XML node and add it to the XML object. When encountering a node of object or array type, the function will call itself recursively, convert the node into an XML element, and add it to the XML element corresponding to the $xml_obj object.

Finally, obtain the XML string format of the created XML object through the asXML method, print it out, and convert it into a string output.

3. Summary

Through the above code, we can easily convert JSON data in PHP into XML data. It should be noted that during the conversion process, the type of data needs to be determined in order to select different elements for creation, and node conversion is performed recursively so that data types such as arrays and objects can be traversed.

The above is the detailed content of How to convert php json to xml format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn