php provides several classes or methods for parsing xml, including: Xml parser, SimpleXML, XMLReader, and DOMDocument.
XML Expat Parser:
XML Parser uses the Expat XML parser . Expat is an event-based parser that treats XML documents as a series of events. When an event occurs, it calls a specified function to handle it. Expat is a validation-free parser that ignores any DTD linked to the document. (Recommended learning: PHP Programming from entry to proficiency)
However, if the form of the document is not good, it will end with an error message. Because it is event-based and has no validation, Expat is fast and suitable for web applications.
The advantage of XML Parser is its good performance, because it does not load the entire xml document into memory and then process it, but processes it while parsing it. But precisely because of this, it is not suitable for those who need to dynamically adjust the XML structure or perform complex operations based on the XML context structure.
If you just want to parse and process a well-structured xml document, then it can complete the task well. It should be noted that XML Parser only supports three encoding formats: US-ASCII, ISO-8859-1 and UTF-8. If your xml data is in other encodings, you need to convert it to one of the above three first.
There are generally two commonly used parsing methods for XML Parser (actually two functions): xml_parse_into_struct and xml_set_element_handler.
xml_parse_into_struct
This method parses the xml data into two arrays:
index array - contains a pointer to the location of the value in the Value array
value array - contains data from the parsed XML
SimpleXML
SimpleXML is a set of simple and easy-to-use xml tools provided after PHP5. Convert xml into objects that are easy to process, and you can also organize and generate xml data. However, it does not apply to xml containing namespace, and the xml must be well-formed.
It provides three methods: simplexml_import_dom, simplexml_load_file, simplexml_load_string. The function name intuitively explains the function of the function. All three functions return SimpleXMLElement objects, and data is read/added through SimpleXMLElement operations.
The advantage of SimpleXML is that it is simple to develop. The disadvantage is that it loads the entire xml into the memory before processing, so it may not be able to parse an xml document with a lot of content. If you are reading small files and the xml does not contain namespace, then SimpleXML is a good choice.
XMLReader
XMLReader is also an extension after PHP5 (installed by default after 5.1). It moves in the document flow like a cursor and stops at each node. , it is very flexible to operate. It provides fast and non-cached streaming access to input, and can read a stream or document, allowing the user to extract data from it, and skip records that are not meaningful to the application.
Use DomDocument to parse XML files
Operation steps:
1, create nodes using the createElement method,
2, create text Use the createTextNode method for content,
3, add child nodes using the appendChild method,
4, and create attributes using the createAttribute method
The above is the detailed content of Several ways for php to parse xml. For more information, please follow other related articles on the PHP Chinese website!