PHP uses DOMDocument to manipulate XML files

Source: Internet
Author: User

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

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.