PHP processing XML

Source: Internet
Author: User
PHP processing XML [IT168] is simple and simple

Unless you have been hiding in the cave for years, you should have heard of XML (it is the toolbox for more and more Web publishers to Mark content for help. You may have even seen XML documents that contain user-defined tags and tags, and you may want to know how a person converts complicated and confusing code into readable content.

The answer is, it is not easy.

Although PHP versions 4.0 already include support for two standard parsing (read: make sense) XML methods (SAX and DOM, the complexity and inherent strangeness of these methods often lead to the abandonment of developers except the most professional XML developers. However, all of this has changed with PHP 5.0. PHP 5.0 introduces a brand new XML extension named SimpleXML, this extension eliminates all (I do mean all) pain points for processing XML documents. Continue reading and find out how it is implemented.

Unpleasant old times

It's time for a short history lesson to understand why SimpleXML is so cool.

Before SimpleXML, there are two ways to process XML documents. The first is a simple API of SAX or XML, which involves traversing an XML document and then calling a specific function when the parser encounters different types of labels. For example, you may have called a function to process a start tag, another function to process the data between the end tag and the third function. The second method is the DOM or Document Object Model, which involves creating a tree in the memory that represents the XML document and then manipulating it using the tree traversal method. Once a specific node of the tree is reached, the corresponding content can be retrieved and used.

These two methods are not particularly easy to use: SAX requires developers to customize the event processor for each type of elements encountered in the XML file, while the DOM method uses the object-oriented mode, in addition to memory-intensive and therefore inefficient for large XML documents, this mode tends to require no developers. In a larger context, PHP 4 uses a large number of different support libraries for different XML extensions, resulting in inconsistent working methods of different XML extensions, then there is a worry about interoperability (as well as a lot of puzzles for developers ).

In PHP 5.0, this issue was fixed by using the libxml2 library (http://www.xmlsoft.org/) as the standard library for all XML extensions and by making different XML extension operations more consistent. Although the biggest XML change in PHP 5 is the SimpleXML extension developed by Sterling Hughes, Rob Richard, and Marcus Börger, however, this extension tries to make parsing XML documents in PHP 5 more friendly than parsing XML documents in PHP 4.

SimpleXML works by converting an XML document into an object and then converting the elements in the document into object attributes that can be accessed using standard object symbols. This makes it easy to obtain any layer of elements in the XML hierarchy to access its content. Duplicate elements at the same level of the document tree are represented as arrays. at the same time, you can use XPath (detailed later) to create a customizable element set; these collections can then be processed using the standard loop structure of PHP. The access element attribute is as simple as the keyword used to access the associated array (no new content needs to be learned, and no special code needs to be written ).

To use SimpleXML and PHP together, your PHP construction must include support for SimpleXML. This type of support is activated by default in PHP 5's UNIX and Windows versions. Read

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.