Detailed explanation of the four methods of php parsing xml
This article mainly introduces the relevant information about the four methods of php parsing xml in detail. Friends in need can refer to
The four methods of php parsing xml
XML processing is often encountered in the development process, and PHP also has rich support for it. This article only briefly explains some of the parsing technologies, including: Xml parser, SimpleXML, XMLReader ,DOMDocument.
1. 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. However, if the document is not in good form, it will end up 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.
XML Parser generally has two commonly used parsing methods (actually two functions): xml_parse_into_struct and xml_set_element_handler.
xml_parse_into_struct
This method parses xml data into two arrays:
index array - contains a pointer to the Value array Pointer to the position of the value
Value array - contains data from the parsed XML
These two arrays are a bit troublesome to describe in words, so let’s look at an example (from PHP official documentation)
$simple = "<para><note>simple note</note></para>"; $p = xml_parser_create(); xml_parse_into_struct($p, $simple, $vals, $index); xml_parser_free($p); echo "Index array\n"; print_r($index); echo "\nVals array\n"; print_r($vals);
Output:
Index array Array ( [PARA] => Array ( [0] => 0 [1] => 2 ) [NOTE] => Array ( [0] => 1 ) ) Vals array Array ( [0] => Array ( [tag] => PARA [type] => open [level] => 1 ) [1] => Array ( [tag] => NOTE [type] => complete [level] => 2 [value] => simple note ) [2] => Array ( [tag] => PARA [type] => close [level] => 1 ) )
The index array has a label named key, and the corresponding value is a Array, which includes the positions of all this tags in the value array. Then through this position, find the value corresponding to this label.
If the format of each set of data in xml is different and cannot be completely unified, then you should pay attention when writing code, you may get wrong results. For example, the following example:
$xml = ' <infos> <para><note>note1</note><extra>extra1</extra></para> <para><note>note2</note></para> <para><note>note3</note><extra>extra3</extra></para> </infos> '; $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $values, $tags); xml_parser_free($p); $result = array();
//下面的遍历方式有bug隐患 for ($i=0; $i<3; $i++) { $result[$i] = array(); $result[$i]["note"] = $values[$tags["NOTE"][$i]]["value"]; $result[$i]["extra"] = $values[$tags["EXTRA"][$i]]["value"]; } print_r($result);
If you traverse in the above way, the code seems simple, But there are hidden dangers, the most fatal of which is getting wrong results (extra3 runs into the second para). So we need to traverse in a more rigorous way:
$result = array(); $paraTagIndexes = $tags['PARA']; $paraCount = count($paraTagIndexes); for($i = 0; $i < $paraCount; $i += 2) { $para = array(); //遍历para标签对之间的所有值 for($j = $paraTagIndexes[$i]; $j < $paraTagIndexes[$i+1]; $j++) { $value = $values[$j]['value']; if(empty($value)) continue; $tagname = strtolower($values[$j]['tag']); if(in_array($tagname, array('note','extra'))) { $para[$tagname] = $value; } } $result[] = $para; }
In fact, I rarely use the xml_parse_into_struct function, so if the so-called "rigorous" code above is not preserved, there will be Bugs in other cases. - -|
xml_set_element_handler
This method is to set the callback function for parser to handle the start and end of elements. Also included is the callback function xml_set_character_data_handler used to set data for the parser. The code written in this way is clearer and easier to maintain.
Example:
$xml = <<<XML <infos> <para><note>note1</note><extra>extra1</extra></para> <para><note>note2</note></para> <para><note>note3</note><extra>extra3</extra></para> </infos> XML; $result = array(); $index = -1; $currData; function charactor($parser, $data) { global $currData; $currData = $data; } function startElement($parser, $name, $attribs) { global $result, $index; $name = strtolower($name); if($name == 'para') { $index++; $result[$index] = array(); } } function endElement($parser, $name) { global $result, $index, $currData; $name = strtolower($name); if($name == 'note' || $name == 'extra') { $result[$index][$name] = $currData; } } $xml_parser = xml_parser_create(); xml_set_character_data_handler($xml_parser, "charactor"); xml_set_element_handler($xml_parser, "startElement", "endElement"); if (!xml_parse($xml_parser, $xml)) { echo "Error when parse xml: "; echo xml_error_string(xml_get_error_code($xml_parser)); } xml_parser_free($xml_parser); print_r($result);
It can be seen that although the set handler method has many lines of code, it has clear ideas and better readability, but the performance It is slightly slower than the first method and not very flexible. XML Parser supports PHP4 and is suitable for systems using older versions. For PHP5 environment, give priority to the following method.
2. SimpleXML
SimpleXML is a set of simple and easy-to-use XML tools provided after PHP5. It can convert XML into objects that are convenient for processing, and 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. All three functions return SimpleXMLElement objects, and data is read/added through SimpleXMLElement operations.
$string = <<<XML <?xml version='1.0'?> <document> <cmd>login</cmd> <login>imdonkey</login> </document> XML; $xml = simplexml_load_string($string); print_r($xml); $login = $xml->login;//这里返回的依然是个SimpleXMLElement对象 print_r($login); $login = (string) $xml->login;//在做数据比较时,注意要先强制转换 print_r($login);
The advantage of SimpleXML is that it is simple to develop. The disadvantage is that it loads the entire xml into memory before processing, so it is difficult to parse an xml document with a lot of content. Sometimes you may feel overwhelmed. If you are reading small files and the xml does not contain namespace, then SimpleXML is a good choice.
3. 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.
Let’s take an example of using the Google Weather API to obtain information to demonstrate the use of XMLReader. Only a small part of the functions are involved here. For more information, please refer to the official documentation.
$xml_uri = 'http://www.google.com/ig/api?weather=Beijing&hl=zh-cn'; $current = array(); $forecast = array(); $reader = new XMLReader(); $reader->open($xml_uri, 'gbk'); while ($reader->read()) { //get current data if ($reader->name == "current_conditions" && $reader->nodeType == XMLReader::ELEMENT) { while($reader->read() && $reader->name != "current_conditions") { $name = $reader->name; $value = $reader->getAttribute('data'); $current[$name] = $value; } } //get forecast data if ($reader->name == "forecast_conditions" && $reader->nodeType == XMLReader::ELEMENT) { $sub_forecast = array(); while($reader->read() && $reader->name != "forecast_conditions") { $name = $reader->name; $value = $reader->getAttribute('data'); $sub_forecast[$name] = $value; } $forecast[] = $sub_forecast; } } $reader->close();
XMLReader和XML Parser类似,都是边读边操作,较大的差异在于SAX模型是一个“推送”模型,其中分析器将事件推到应用程序,在每次读取新节点时通知应用程序,而使用XmlReader的应用程序可以随意从读取器提取节点,可控性更好。
由于XMLReader基于libxml,所以有些函数要参考文档看看是否适用于你的libxml版本。
4。 DOMDocument
DOMDocument还是PHP5后推出的DOM扩展的一部分,可用来建立或解析html/xml,目前只支持utf-8编码。
$xmlstring = <<<XML <?xml version='1.0'?> <document> <cmd attr='default'>login</cmd> <login>imdonkey</login> </document> XML; $dom = new DOMDocument(); $dom->loadXML($xmlstring); print_r(getArray($dom->documentElement)); function getArray($node) { $array = false; if ($node->hasAttributes()) { foreach ($node->attributes as $attr) { $array[$attr->nodeName] = $attr->nodeValue; } } if ($node->hasChildNodes()) { if ($node->childNodes->length == 1) { $array[$node->firstChild->nodeName] = getArray($node->firstChild); } else { foreach ($node->childNodes as $childNode) { if ($childNode->nodeType != XML_TEXT_NODE) { $array[$childNode->nodeName][] = getArray($childNode); } } } } else { return $node->nodeValue; } return $array; }
从函数名上看感觉跟JavaScript很像,应该是借鉴了一些吧。DOMDocument也是一次性将xml载入内存,所以内存问题同样需要注意。PHP提供了这么多的xml处理方式,开发人员在选择上就要花些时间了解,选择适合项目需求及系统环境、又便于维护的方法。
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
The above is the detailed content of Detailed explanation of the four methods of php parsing xml. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
