PHP processing XML

Source: Internet
Author: User
Tags php foreach
"IT168 Zhuangao" simple, quite simple

Unless you've been hiding in caves in recent years, you should have heard of XML (it's a toolkit that more and more web publishers are asking for content tagging). You may even have seen XML documents that contain user-defined tags and tags in practice, and you might want to know how a person can translate complex and confusing code into human-readable content.

The answer is, it's not easy.

Although PHP has included support for parsing (reading: becoming meaningful) XML methods (sax and DOM) for both standards since version 4.0, the complexity and innate quirks of these methods often make it possible for developers other than the most professional XML developers to give up. However, all of this has changed with PHP 5.0, and PHP 5.0 introduces a new XML extension called SimpleXML, which eliminates all (and I really mean all) pain in working with XML documents. Continue reading and find out how it is implemented.

An unpleasant old time.

To understand why SimpleXML is so cool, it's time to go to a short history lesson.

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

Neither of these methods is particularly easy to use: Sax requires the developer to customize the event handler for each type of element encountered in the XML file, whereas the DOM method uses object-oriented mode, which is memory-intensive and therefore inefficient for large XML documents and tends not to require developers. In a larger context, PHP 4 uses a large number of different support libraries for its different XML extensions, resulting in inconsistencies in how different XML extensions work, and thus creating interoperability concerns (and a lot of confusion for developers).

In PHP 5.0, this problem is fixed by using the LIBXML2 Library (http://www.xmlsoft.org/) as the standard library for all XML extensions and by making different XML scaling operations more consistent. While the biggest change in XML in PHP 5 is the simplexml extension developed by Sterling Hughes, Rob Richards, and Marcus Börger, the extension attempts to make parsing XML documents in PHP 5 more friendly than parsing XML documents in PHP 4.

SimpleXML works by transforming an XML document into an object and then transforming the elements within that document into object properties that can be accessed using standard object symbols. This makes it easy to get the elements of any layer in the XML hierarchy down to access its contents. Duplicate elements at the same level of the document tree are represented as arrays, while customizable elements collections can be created using XPath (detailed later) location paths, which can then be handled using PHP's standard looping structure. It is as simple as accessing the element properties and accessing the associated array's keywords (there is no new content to learn, and there is no special code to write).

In order to use SimpleXML and PHP together, your PHP build must include support for SimpleXML. This support is activated by default in versions of UNIX and Windows platforms in PHP 5. Read more about this and go to http://www.php.net/manual/en/ref.simplexml.php. If you are a PHP 4 user, then you have bad luck, simplexml only available in PHP 5.

Children's Zoo

To figure out how SimpleXML works, consider the following XML file:

? XML version = "1.0"?> < pet > < name > Polly Parrot < age > 3 < species > pa Rrot < parents > < mother > Pia Parrot < father > Peter Parrot

Now, you need a way to get the content that is contained in ,,, and between elements. It's too easy to use the SimpleXML:

Name. "\ n"; echo "Age:". $xml, age. "\ n"; echo "Species:". Species, $xml. "\ n"; echo "Parents:". Parents, mother, $xml. "and". Parents, father, $xml. "\ n";?>

The action begins with the simplexml_load_file () function, which accepts the path and name of the parsed XML file. The result of the file parsing is a PHP object that has properties that correspond to the elements below the root element. character data within an element can thus be accessed using the standard object-and-attribute notation, which is accessed first from the root element and then down the hierarchical path of the file.

As you can read the operation, so you can also write operations. SimpleXML makes it easy to modify the contents of a particular XML element (simply assigning a new value to the corresponding object property). Here is an example:

Name = "Sammy Snail"; $xml, age = 4; $xml-species = "snail"; Parents, $xml, mother = "Sue Snail"; Parents, $xml-father = "Sid Snail"; Write new data to file File_put_contents ($file, $xml-asxml ());?>

Here, the original XML file is first read in, and then the character data inside each of its elements is modified by assigning a new value to the corresponding object property. Typically, the Asxml () method, which is used to output an XML tree to a standard output device, combines the file_put_contents () function in this instance to overwrite the original XML document with the new data.

City of Sin

Duplicate elements at the same level in the XML hierarchy are represented by array elements and can therefore be accessed using a numeric index. To understand how this works, consider the following XML file:

< sins > < sin > Pride < sin > Envy < sin > Anger < sin > Greed < sin > sloth < sin > gluttony < sin > Lust

Here is the PHP script that reads the XML file and then gets the data from it:

echo $xml sin[0]. "\ n"; echo $xml sin[1]. "\ n"; echo $xml sin[2]. "\ n"; echo $xml sin[3]. "\ n"; echo $xml sin[4]. "\ n"; echo $xml sin[5]. "\ n"; echo $xml sin[6]. "\ n";?>

If you like, you can even use the foreach () loop to iterate over the collection, just like the next equivalent list:

Element collection foreach ($xml, sin as $sin) {echo "$sin \ n";}? >

The way things look in the future

SimpleXML can manipulate element properties and their contents as transparently as the elements they manipulate. A property-value pair is represented as a member of the PHP associative array and can be accessed as if it were a regular array element. To see how this works, take a look at the following script:

!--? PHP//create XML string $str = <<< XML. xml version = "1.0"?--> < shapes > < Sha PE type = "Circle" radius = "2"/> < shape type = "Rectangle" length = "5" width = "2"/> < shape ty PE = "Square" length = "7"/>!--shapes--> XML; Load string $xml = Simplexml_load_string ($str) or Die ("Unable to load XML string!"); For each shape//Calculate area foreach ($xml, shape as $shape) {if ($shape [' type '] = = "Circle") {$area = Pi () * $shape [' radius '] * $shape [' radius ']; } elseif ($shape [' type '] = = "Rectangle") {$area = $shape [' Length '] * $shape [' width '];} ElseIf ($shape [' type '] = = "Square") {$area = $shape [' Length '] * $shape [' length '];} Echo $area. "\ n";}? >

The

differs from the previous example of using an external XML file, which dynamically creates an XML document and then loads it into the simplexml using the Simplexml_load_string () method. The XML is then parsed using the foreach () loop and the area of each of its shapes is computed based on the value of the Type property of each element. The above list shows how the property values are accessed as keywords for the attribute array associated with each element property. The

X represents the point

SimpleXML also supports customizing the collection of elements through the XPath location path. For those who are new to XML, XPath is a standard XML document addressing mechanism that allows developers to access a collection of elements, attributes, or text nodes within a document. To read more about XPath, please visit http://www.w3.org/TR/xpath.html and http://www.melonfire.com/community/columns/trog/article.php? id=83
To understand how it works, consider the following XML document:

!--? XML version = "1.0"?--> < Ingredients > < item > < DESC > Boneless CHICKEN Breasts!--desc--> < quantity > 2!--quantity-->!--Item--> < item > < desc > Chopped on Ions!--desc--> < quantity > 2!--quantity-->!--Item--> < item > < desc > Ginger!-- Desc--> < Quantity > 1!--quantity-->!--Item--> < item > < desc > Garlic!--desc--> < ; Quantity > 1!--quantity-->!--Item--> < item > < desc > Red chili powder!--desc--> < Qu Antity > 1!--quantity-->!--Item--> < item > < desc > coriander seeds!--desc--> < Quant ity > 1!--quantity-->!--Item--> < item > < desc > Lime Juice!--desc--> < quantity > 2!--Quantity-->!--item-->!--Ingredients-->

Now, let me assume you want to print all the elements. You can print an element by enumerating an array of elements, as discussed earlier, or you can use the XPath () method to create only a custom set of elements, and then repeat it:

Elements and print foreach ($xml, XPath ('//desc ') as $desc) {echo "$desc \ n";}? >

Using XPath, you can even (for example) be more imaginative than this by creating a collection of only elements (which correspond to a number of 2 or more).

Elements and print foreach ($xml, XPath ('//item[quantity > 1]/desc ') as $desc) {echo "$desc \ n";}? >

Without XPath, it would be much more complex to do this than the five lines of code above. You test it yourself and see if it is!

One night at the Moulin Rouge

Now that you understand what XPath can do, let's end with an example of how you might actually use XPath.
Let's assume that you have a series of XML-labeled movie reviews as follows:

< review ID = "$" category = "2" > < title > Moulin Rouge < teaser > Baz Luhrmann ' s over-the-top vision of Paris at the turn of the century are witty, Sexy...and completely Unforgettable < cast > < person > Nicole Kidman < person > Ewan McGregor < person > John Leguizamo < person > Jim Broadbent < person > Richard Roxburgh < director > Baz Luhrmann < duration > 120 < genre > Romance/comedy < year > 2001 < BODY > A stylishly spectacular Extravaganza, Moulin Rouge is hard to categorize; It is, at the different times, a love story, a costume drama, a musical, and a comedy. Director Baz Luhrmann (well-known for the very hip William Shakespeare ' s Romeo + Juliet) have taken some simple themes -Love, jealousy and obsession-and do something completely new and different with them by setting them to music. < rating > 5

Now, you're going to show the comment on your Web site. So you need a PHP script to extract the data from the file and place it in the appropriate place in the HTML template. This is easy, as the following code demonstrates, as far as what you have learned:

< HTML > < head >< basefont face = "Arial" > < BODY > < H1 > title;?> ( Year;?>) < h3 > Teaser;?> Body;?> < p align = "right"/> < font size = "2" > Director: < b > Director;?> < br/> Duration: < b > Duration;?> min. < br/> Cast: < b > Cast, person as $person) {echo "$person";}? > < br/> Rating: < b > Rating;?>

  • 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.