Using DOMDocument to read an XML file requires the following methods and properties:
Method:
1: Read XML file: Load ()
2: Array of objects getting tags: getelementbytagname ()
3: Index of Object array: item ()
Property:
1: Get the text of a property or node: NodeValue
2: Get the attribute name or node name: NodeName
2: Get the child node collection for this node: childNodes
3: Gets the collection of properties for this node: attributes
instance XML file (a.xml):
1 <?XML version= "1.0 encoding=" UTF-8 "?>2 <peopleNation= "Han" City= "Mars">3 <XiaohuaCollege= "Qinghua">4 <name>Xiaohua</name>5 < Age>12</ Age>6 <Sex>Man</Sex>7 </Xiaohua>8 <xiaomingCollege= "Beida">9 <name>Xiao ming</name>Ten < Age>15</ Age> One <Sex>Man</Sex> A </xiaoming> - <XiaoqiangCollege= "Jiaotong"> - <name>Jack bauer</name> the < Age>13</ Age> - <Sex>Man</Sex> - </Xiaoqiang> - </people>
PHP uses DOMDocument to manipulate XML files
<?PHP$dom=NewDomDocument (); $dom->load (A.XML); $people=$dom->getelementbytagname (' People ');//gets an array of people nodes
$people->item (0);//Gets the first people node $people->item (0)->childnodes;//get all child nodes of the first people node $people->item (0)->attributes;//get all the properties of the first People node
$people->item (0)->childnodes->item (0);//gets the first node of the first People node, the Xiaohua node $people->item (0)->attributes->item (0);//gets the first property of the first people node, which is the nation property. $people->item (0)->childnodes->item (0)->childnodes->item (0);//Gets the name node in the first node of the first people node
Echo $people->item (0)->attributes->item (0)->nodename;//output string: Nation Echo $people->item (0)->attributes->item (0)->nodevalue//output string: Han Echo $people->item (0)->childnodes->item (0)->childnodes->item (0)->nodename;//Output string: Name Echo $people->item (0)->childnodes->item (0)->childnodes->item (0)->nodevalue;//output string: Xiao Hua
foreach ($people->item (0)->attributes as $key + = $value) {//Traverse node
echo $key;//First output string: Nation, second output string: City
echo $value->nodevalue;//First output string: Han, second output string: Mars
}
PHP uses DOMDocument to manipulate XML files