Xml dom (10) and xmldom in php
1. php dom (1)
The DOM in Php is different from that in javascript. You do not need to add another node to the attribute.
2.Main Category
DOMDocument: document class
DOMNodeList: node list class
DOMNode: node class
DOMElement: Element class
3. DOMDocumentClass
3.1 create Document Object
DOMDocument ::__ construct ([string $ version [, string $ encoding])
[String $ version: version Number
[, String $ encoding]: Character Set
3.2. load xml files
Mixed DOMDocument: load (string $ filename)
String $ filename: name of the xml file to be loaded
3.3 obtain a node
DOMNodeList DOMDocument: getElementsByTagName (string name)
String name: name of the node to be retrieved
Example: Demo. php
<? Php header ('content-Type: text/html; charset = gb2312 '); // parse demo01.xml using php // instantiate the dom object $ dom = new DOMDocument (); // load the xml file $ dom-> load ('demo01. xml '); // get the person node through the dom object. Note: The returned value here is a DOMNodeList Class Object $ persons = $ dom-> getElementsByTagName ('person '); // There are several person echo 'Total '. $ persons-> length. 'individuals <br> '; // two people are output. // select the person with the index 0, that is, the first person $ person = $ persons-> item (0 ); // get the name node under this person, and the returned result is still a D OMNodeList Class Object $ names = $ person-> getElementsByTagName ('name'); // output name echo $ names-> item (0)-> nodeValue. '<br>'; // output 'zhang san'. Why do we need to perform two Nodelist operations? Please refer to because there are two nodes
Demo. xml
<?xml version="1.0" encoding="UTF-8"?><persons> <person id="s101"> <name>zhangsan</name> <age>30</age> </person> <person> <name>wangwu</name> <age>18</age> </person></persons>
4. DOMNodeList class (the above person is the NodeList node)
1. Length
The current node list contains several nodes.
2. DOMElement DOMNodelist: item (int $ index)
Select the node whose index is
Int $ index: index
5, DOMNode class
1. nodeValue node Value
6Read attributes
1. bool DOMElement: hasAttribute (string $ name)
Determine whether an attribute exists (only whether an attribute exists)
String $ name: attribute name
2. bool DOMNode: hasAttributes (void)
Determines whether an attribute exists (true is returned if an attribute exists)
3. string DOMElement: getAttribute (string $ name)
Obtains the attribute value of a specified attribute.
String $ name: attribute name
Example 2. output with coherent operation
<? Php $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> load ('demo01. xml'); // get the first person's name echo $ dom-> getElementsByTagName ('person ')-> item (0)-> getElementsByTagName ('name ') -> item (0)-> nodeValue; output: John
Example3:
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> load ('demo01. xml '); $ person = $ dom-> getElementsByTagName ('person')-> item (0 ); // determine whether a node has the id attribute if ($ person-> hasAttribute ('id') {echo 'has the id attribute';} else {echo 'has no id attribute ';} echo '<br>'; // determines whether a node has an attribute. if ($ person-> hasAttributes () {echo 'has an attribute ';} else {echo 'has no attribute';} echo '<br>'; // obtain the id attribute value echo $ person-> getAttribute ('id') of the first person ');
In the previous example, only one piece of data can be obtained. Now, how can I get complete information from all users?
Example 4:
<? Php header ('content-type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> load ('demo01. xml '); // get all person nodes $ persons = $ dom-> getElementsByTagName ('people'); // get the total number of people $ count = $ persons-> length; for ($ I = 0; $ I <$ count; $ I ++) {// $ person indicates the I individual $ person = $ persons-> item ($ I ); echo 'dd '. ($ I + 1 ). 'Personal '; if ($ person-> hasAttribute ('id') {echo 'student id :'. $ person -> GetAttribute ('id');} echo 'name :'. $ person-> getElementsByTagName ('name')-> item (0)-> nodeValue; echo 'Age :'. $ person-> getElementsByTagName ('age')-> item (0)-> nodeValue; echo '<br>' ;}?>