Home > Article > Backend Development > How to convert xml into array in php
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:
$xml = simplexml_load_file('example.xml');
$array = json_decode(json_encode($xml), TRUE);
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.
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:
$dom = new DOMDocument(); $dom->load('example.xml');
$root = $dom->documentElement;
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; } }
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.
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:
$xml_parser = xml_parser_create();
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");
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.
$fp = fopen('example.xml', 'r'); while ($data = fread($fp, 4096)) { xml_parse($xml_parser, $data, feof($fp)); }
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.
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!