1.PHP DOM (1)
Dom in PHP is not the same as JavaScript, and attributes do not have to add another node
2. Main classes
DOMDocument: Document class
Domnodelist: Node List class
Domnode: Node class
DomElement: Element class
3. DOMDocument class
3.1 Creating a Document object
Domdocument::__construct ([String $version [, String $encoding])
[String $version: Version number
[, String $encoding]] : Character Set
3.2. Loading the XML file
Mixed Domdocument::load (string $filename)
String $filename: The name of the XML file to load
3.3. Get the Node
Domnodelist domdocument::getelementsbytagname (string name)
String Name: The name of the node to get
Example: demo.php
PHP Header ('content-type:text/html;charset=gb2312' );//parsing demo01.xml with PHP//instantiating a DOM object$dom =NewDOMDocument ();//loading an XML file$dom->load ('Demo01.xml' );//get the person node through the DOM object, note: Here The return value is a Domnodelist class object$persons = $dom->getelementsbytagname (' Person' );//There are several personEcho'Total'. $persons->length.'Personal
';//output total of two people//Select Person with index 0, which is the first one$person = $persons->item (0 );//Gets the name node under this person, and the return is still a Domnodelist class object$names = $person->getelementsbytagname ('name' ); //Output NameEcho $names->item (0)->nodevalue.'
';//output ' Zhang San 'Why do we have to take the nodelist two times? Please refer to because there are two nodes
Demo.xml
"1.0" encoding="UTF-8"?>
"
S101
"
> Zhangsan
wangwu
4. Domnodelist class (The person above is the nodelist node)
1. Length
There are several nodes in the current node list
2, domelement domnodelist::item (int $index)
Select the node indexed as index
int $index: Index
5, Domnode class
1, the value of the NodeValue node
6, about the property read
1, bool Domelement::hasattribute (string $name)
Determine if there is a property (only one property is judged to have one)
String $name: Property name
2, bool Domnode::hasattributes (void)
Determine if there is a property (as long as the property returns True)
3, String Domelement::getattribute (String $name)
Gets the property value of the specified property
String $name: Property name
Example 2. Output with coherent operation
PHP new'1.0'utf-8' ); $dom'demo01.xml' ); // want the name of the first person ' Person ' 0 ) 'name'0 ,nodevalue; output: Zhang San
Example3:
PHP Header ('content-type:text/html;charset=gb2312' ); $dom=NewDOMDocument ('1.0','Utf-8' ); $dom->load ('Demo01.xml' ); $person= $dom->getelementsbytagname (' Person')->item (0 );//To determine if a node has an id attribute if($person->hasattribute ('ID') {echo'with id attribute'; } Else{echo'No id attribute'; } Echo'
';//determine if a node has attributes if($personhasattributes ()) {echo'has attributes'; } Else{echo'does not have any attributes'; } Echo'
';//Gets the id attribute value of the first personEcho $person->getattribute ('ID');
The previous example can only get a single piece of data, now I want to get all the full information, how to achieve?
Example 4:
What if this property has to be traversed?
PHP Header ('content-type:text/html;charset=gb2312' ); $dom=NewDOMDocument ('1.0','Utf-8' ); $dom->load ('Demo01.xml' );//Get all person nodes$persons = $dom->getelementsbytagname (' Person' );//total number of persons received$count = $personslength; for($i =0; $i < $count; $i + +) { //$person represents the first person$person = $personsitem ($i); Echo'Section'. ($i +1) .'Individual's'; if($person->hasattribute ('ID') {echo'School Number:'. $person->getattribute ('ID' ); } Echo'Name:'. $person->getelementsbytagname ('name')->item (0),NodeValue; Echo'Age:'. $person->getelementsbytagname (' Age')->item (0),NodeValue; Echo'
'; }?>
The above describes the XML DOM (10) in PHP, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.