Example 1: Loop all "artist" elements (NodeTypes = 1), find information about the correct artist output album The CD contains with the data that the JavaScript is sending to the matching name
XML file:
<CATALOG> <CD> <title>empire burlesque</title> <artist>bob dylan</artist> < country>usa</country> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <year >1985</YEAR> </CD> <CD> <title>hide your heart</title> <artist>bonnie Tyler </ARTIST> <COUNTRY>UK</COUNTRY> <company>cbs records</company> <PRICE>9.90< /price> <YEAR>1988</YEAR> </CD> ········· </CATALOG> |
XML file Analysis:
PHP File: Step: Loop All "artist" elements (NodeTypes = 1), find information about the correct artist output album The CD contains with the data that the JavaScript is sending to the matching name
<?php $q =$_
get["Q"];
$xmlDoc = new DOMDocument ();
$xmlDoc->load ("Cd_catalog.xml"); $x = $xmlDoc->getelementsbytagname (' ARTIST '); Find artist node for ($i =0 $i <= $x->length-1; $i + +) {//process only ELEMENT nodes if ($x->item ($i)->no by artist label detype==1)//"Note 1" {if ($x->item ($i)->childnodes->item (0)->nodevalue = = $q)//"Comment 2" {$y = ($x-
>item ($i)->parentnode);
{}}} $CD = ($y->childnodes); For ($i =0 $i < $CD->length; $i + +) {//process only ELEMENT nodes if ($CD->item ($i)->nodetype==1) {echo ($CD
->item ($i)->nodename);
Echo (":");
Echo ($CD->item ($i)->childnodes->item (0)->nodevalue);
Echo ("<br/>"); |
Note 1 If the node is an element node, the NodeType property returns 1, and if it is an attribute node, the NodeType property returns 2. This method excludes the non element nodes. $x gets an array of artist nodes, because the entire XML file has multiple artist nodes, so use item (index) to determine which artist node is currently in the order in which they appear. $x->item (0) represents the first artist label that appears. "Note 2" The $i+1 of the artist node is text, so nodevalue is the corresponding text content.
Example 2:me.xml
<?xml version= "1.0" encoding= "Utf-8"?> <phplamp> <post> <title id= "1" >php XML processing Introduction </title> <details> detailed content One </details> </post> <post> <title id= "2" >php XML processing Introduction two </title> <details> details two </details> </post> <post> <title id= "3" >php XML processing Introduction III </title> <details> detailed content three </details> </post> </phplamp>
|
Php
The first thing to build is a DOMDocument object $xml = new DOMDocument (); Load XML file $xml->load ("Me.xml"); Get all the post labels $postDom = $xml->getelementsbytagname ("post"); Looping through post labels foreach ($postDom as $post) { Get title Tag node $title = $post->getelementsbytagname ("title"); /** * To get the id attribute of the title tag, divide it into two parts. * 1. Gets a list of all the attributes in title, which is $title->item (0)->attributes * 2. Gets the property of the ID in title because it is in the first place with item (0) * * Tips: * If you take the value of the attribute can be->nodevalue with item (*) * If you take the label of the attribute, you can use item (*)->nodename * If the type of attribute can be->nodetype with item (*) */ echo "Id:". $title->item (0)->attributes->item (0)->nodevalue. "<br/>"; echo "Title:". $title->item (0)->nodevalue. "<br/>"; echo "Details:". $post->getelementsbytagname ("Details")->item (0)->nodevalue. "<br/><br/>"; } |
Summary: The approximate way to parse an XML file using DOMDocument is: 1. First build a DOMDocument object: $xml = new DOMDocument (); 2. Load XML file: $xml->load (" XMLFile.xml "); 3. Obtain the corresponding label: $xml->getelementsbytagname (" A "); NOTE: 1> This is not a specific label, but this kind of label, the equivalent of the tag array, so to access that specific label, but also through the item (index) to get to the first index+1 tag. 2> through $xml->item (insex) can get a tag, such as Get the following a tag: <a id = "ia" name= "NA" > <B1 id= "B1" >1 </B1> <B2 id= "B2" >2</B2> & nbsp <A> A This tab has two attributes and two direct subnodes. Get a property: $xml->item (insex)->attributes &N Bsp Gets the first property ID: $xml->item (insex)->attributes->item (0); if: Echo $xml->item (insex)->attri Butes->item (0); OUTPUT result: ID Get the value of the property: $xml->item ( Insex)->attributes->item (0)->nodevalue; if: Echo $xml->item (inse x)->attributes->item (0)->nodevale; The output result is: IA Get child node: $xml->item ( Insex)->childnodes Get first child node B1: $xml->item (insex)-> Childnodes->item (0); if: Echo $xml->item (inse x)->childnodes->item (0); Output is: B1 Get the value of the property: $xml->item (insex)-> Childnodes->item (0)->nodevalue; &NBSP if: Echo $xml->item (insex)->childnodes->item (0)-> nodevale; The output is: 1 but item (1) is not necessarily corresponding to the next label, so there is an example 1 of the NodeType in the judgment.
The DOM item () method is detailed: Http://www.w3school.com.cn/jsref/met_nodelist_item.asp Dom NodeType Properties http://www.w3school.com.cn/ Jsref/prop_node_nodetype.asp