Home > Backend Development > PHP Problem > How to convert xml into array in php

How to convert xml into array in php

PHPz
Release: 2023-04-24 11:41:30
Original
2725 people have browsed it

In the process of programming with PHP, XML files are often used, but not all developers are familiar with the syntax rules of XML files, so the XML files need to be converted into arrays for processing. Here's how to convert an XML file into an array using PHP.

1. Use the SimpleXML extension function

The SimpleXML extension function is an XML parsing extension that comes with PHP. It can parse the XML file into an object and then convert the object into an array.

The steps to use the SimpleXML extension function are as follows:

  1. Use the simplexml_load_file() function to load the XML file. The sample code is as follows:
$xml = simplexml_load_file('example.xml');
Copy after login
  1. SimpleXML objects are converted into arrays. The sample code is as follows:
$array = json_decode(json_encode($xml), TRUE);
Copy after login

Among them, the json_decode() function is a function that converts JSON format strings into PHP arrays. The json_encode() function serializes a PHP array into a JSON format string. Here we serialize the SimpleXML object into a JSON format string and then parse it into a PHP array.

  1. Process the converted array and implement business logic.

2. Use the DOMDocument class

DOMDocument is a DOM parsing extension that comes with PHP. It can parse XML files into a tree structure, and then find the nodes we need. deal with.

The steps to use the DOMDocument class are as follows:

  1. Use the load() method to load the XML file. The sample code is as follows:
$dom = new DOMDocument();
$dom->load('example.xml');
Copy after login
  1. Get the root Node, the sample code is as follows:
$root = $dom->documentElement;
Copy after login
  1. Traverse each child node under the root node and convert the name and value of the node into an array.

The sample code is as follows:

$array = array();
if ($root->hasChildNodes()) {
    $children = $root->childNodes;
    for ($i = 0; $i < $children->length; $i++) {
        $child = $children->item($i);
        $array[$child->nodeName] = $child->nodeValue;
    }
}
Copy after login

In the above code, we first create an empty array, and then traverse each child node under the root node. If the child node is not empty, then we store its name and value in an array.

  1. Process the converted array and implement business logic.

3. Use the XML Parser class

XML Parser is an XML parser that comes with PHP. It can parse XML files into an event stream, and then we can parse the XML file into an event stream according to the type of event. to process this XML file.

The steps to use XML Parser are as follows:

  1. Create an XML parser object, the sample code is as follows:
$xml_parser = xml_parser_create();
Copy after login
  1. Set the event processing function, The sample code is as follows:
function startElement($parser, $element_name, $element_attrs) {
    //处理元素开始事件
}

function endElement($parser, $element_name) {
    //处理元素结束事件
}

function charData($parser, $data) {
    //处理文本内容事件
}

xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData");
Copy after login

In the above code, we define three processing functions: startElement() is used to process the element start event, endElement() is used to process the element end event, charData() Used to handle text content events. Then we register these three processing functions into the XML parser.

  1. Parse XML files, the sample code is as follows:
$fp = fopen('example.xml', 'r');
while ($data = fread($fp, 4096)) {
    xml_parse($xml_parser, $data, feof($fp));
}
Copy after login

In the above code, we first read the XML file into the memory in binary mode, and then gradually transfer the data blocks Parsed by XML parser.

  1. Process the converted array and implement business logic.

Summary:

The above are three methods of converting XML files into arrays. Developers can choose the appropriate method according to actual needs. In the process of using the SimpleXML extension function and the DOMDocument class, PHP will occupy a large amount of memory and CPU resources, while using the XML Parser class can save memory and CPU resources, but requires writing more processing functions.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template