In the PHP development process, XML processing is a very common operation, and PHP provides many functions and class libraries for XML processing. Among them, the function of converting XML documents into PHP arrays is a very important operation, because it can easily use XML data for subsequent operations and logical analysis. This article will discuss some commonly used functions in PHP for converting XML documents into PHP arrays.
1. simplexml_load_string()
simplexml_load_string() is a commonly used function in PHP, which can convert XML documents into simple XML objects in PHP. This function receives an XML document as a string and creates a simple XML object by parsing the document. The following is a simple example:
$xml_string = "<students><student><name>Tom</name><age>18</age></student></students>"; $xml_object = simplexml_load_string($xml_string); print_r($xml_object);
The above code will output the following results:
SimpleXMLElement Object ( [student] => SimpleXMLElement Object ( [name] => Tom [age] => 18 ) )
In the above example, we use an XML string as a parameter of the simplexml_load_string() function, and Assign the returned object to the $xml_object variable. We found that the value of $xml_object is a SimpleXMLElement object, which contains all the data in the XML document.
It should be noted that the simplexml_load_string() function is usually used for simple XML document processing. If your XML document is more complex, it is recommended to use other XML processing functions and class libraries.
2. simplexml_load_file()
The simplexml_load_file() function is very similar to the simplexml_load_string() function. The only difference is that the former parses the XML document from the PHP file system. The following is a simple example:
$xml_file = "students.xml"; $xml_object = simplexml_load_file($xml_file); print_r($xml_object);
The above code will output the following results:
SimpleXMLElement Object ( [student] => Array ( [0] => SimpleXMLElement Object ( [name] => Tom [age] => 18 ) ) )
In the above example, we use the simplexml_load_file() function to parse an XML file into a SimpleXMLElement object And assign it to the $xml_object variable. This function facilitates parsing XML files from the file system, but you also need to be aware of the complexity of XML files.
3. xml_parse_into_struct()
xml_parse_into_struct() function is a very commonly used function in PHP, which can parse XML documents into PHP arrays. Different from the simplexml_load_string() function and simplexml_load_file() function, the xml_parse_into_struct() function returns a two-dimensional array that contains all elements and attributes in the XML document. The following is a simple example:
$xml_string = "<students><student><name>Tom</name><age>18</age></student></students>"; $xml_parser = xml_parser_create(); xml_parse_into_struct($xml_parser, $xml_string, $xml_array); xml_parser_free($xml_parser); print_r($xml_array);
The above code will output the following results:
Array ( [0] => Array ( [tag] => students [type] => open [level] => 1 ) [1] => Array ( [tag] => student [type] => open [level] => 2 ) [2] => Array ( [tag] => name [type] => complete [level] => 3 [value] => Tom ) [3] => Array ( [tag] => age [type] => complete [level] => 3 [value] => 18 ) [4] => Array ( [tag] => student [type] => close [level] => 2 ) [5] => Array ( [tag] => students [type] => close [level] => 1 ) )
In the above example, we use the xml_parser_create() function to create an XML parser, and then use The xml_parse_into_struct() function parses an XML string into a PHP array and assigns it to the $xml_array variable. Finally, we use the print_r() function to output the value of the $xml_array array.
It should be noted that the xml_parse_into_struct() function is only suitable for parsing small XML documents. For large and complex XML documents, it is recommended to use other XML parsing functions or libraries.
4. DOMDocument class
The DOMDocument class is a very commonly used and powerful XML parser in PHP. It can parse XML documents into DOM objects and provides a set of APIs to access and modify the nodes and properties of this object. The following is a simple example:
$xml_string = "<students><student><name>Tom</name><age>18</age></student></students>"; $dom_document = new DOMDocument(); $dom_document->loadXML($xml_string); $students = $dom_document->getElementsByTagName('student'); foreach ($students as $student) { $name = $student->getElementsByTagName('name')->item(0)->nodeValue; $age = $student->getElementsByTagName('age')->item(0)->nodeValue; echo "Name: $name, Age: $age"; }
The above code will output the following results:
Name: Tom, Age: 18
In the above example, we use the DOMDocument class to parse the XML string into a DOM object, and use The getElementsByTagName() function selects the student node. Finally, we use the nodeValue property to get the value of the node and output it to the screen.
It should be noted that the DOMDocument class provides a wealth of methods and attributes for operating and modifying XML documents, making it the best choice for XML parsing and processing.
Summary
In this article, we have introduced in detail some common functions and class libraries in PHP for converting XML documents into PHP arrays. These functions and class libraries can easily use XML data for subsequent operations and logical analysis. In actual use, depending on the complexity of the XML document and the usage scenarios, we can choose different functions and class libraries to complete our XML processing tasks.
The above is the detailed content of How to convert XML document to array in PHP. For more information, please follow other related articles on the PHP Chinese website!

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
