When using
The XML file used is also an example of XML in the article "How to use PHP XML to Process SimpleXML", the file name is: Me.xml. The code is as follows:
PHP XML processing XML code
- < ? xml version = "1.0" encoding="utf-8"?>
- < phplamp >
- < post >
- < title ID="1">PHP XML processing Introduction < /title >
- < details > detail one < /details>
- < /post >
- < post >
- < title ID="2">PHP XML processing Introduction two < /title >
- < details> content two < /details>
- < /post >
- < post >
- < title ID="3">PHP XML processing Introduction three < /title >
- < details > content three < /details>
- < /post >
- < /phplamp >
You then need to use PHP DOMDocument to process the file and parse any elements in the XML file. The code is as follows, with comments inside.
PHP DOMDocument parsing xml file code
- < ? PHP
- The first thing to do is to build a DOMDocument object
- $ XML = New DOMDocument ();
- Loading an XML file
- $xml- > load ("Me.xml");
- Get all the post labels
- $ Postdom = $xml- >
getElementsByTagName ("post");
- Looping through post labels
- foreach ($postDom as $post) {
- Get the title Tag node
- $ title = $post- >
getElementsByTagName ("title");
- /**
- * To get the id attribute of the title tag, go to section two.
- * 1. That gets all the attributes in the title.
List is $title->Item (0)->attributes
- * 2. Gets the property of the ID in the title,
Because it's in the first place, so with item (0)
- *
- * Tip:
- * If the value of the attribute can be taken with item (*)- > NodeValue
- * If the attribute tag can be used with item (*)- > NodeName
- * If the type of the attribute can be used with item (*)- > NodeType
- */
- echo "Id:". $title->Item (0)->
attributes- > Item (0)- > NodeValue. "< br />";
- echo "Title:". $title->
Item (0)- > NodeValue. "< br />";
- echo "Details:". $post->
getElementsByTagName ("Details")- > I
TEM (0)->nodevalue. "< br /> < BR />";
- }
- ?>
This is just a way, PHP DOMDocument is quite powerful, there are more analytic methods and strategies, waiting for you to explore.
http://www.bkjia.com/PHPjc/446191.html www.bkjia.com true http://www.bkjia.com/PHPjc/446191.html techarticle The XML file used in the use of XML is also used in PHP XML processing of the SimpleXML use of the text in the case of a simple example, the file name is: Me.xml. The code is as follows: PHP XML processing XML code? XML Versi ...