XMLDOM (10) in php)

Source: Internet
Author: User
: This article mainly introduces XMLDOM (10) in php. if you are interested in the PHP Tutorial, refer to it. 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 classes

DOMDocument: Document class

DOMNodeList: node list class

DOMNode: node class

DOMElement: element class

3. DOMDocument class

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

 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
'; // Output a total of two people // select the person whose index is 0, that is, the first person $ person = $ persons-> item (0 ); // Get the name node under this person and return a DOMNodeList class object $ names = $ person-> getElementsByTagName ('name '); // Output name echo $ names-> item (0)-> nodeValue.'
'; // Output 'Zhang San'. why do we need to perform two Nodelist operations? Please refer to because there are two nodes

Demo. xml

 
     
          
   
    zhangsan
           30    
      
          
   
    wangwu
           18    
  
 

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

6. read 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

 Load ('demo01. XML'); // get the first person's name echo $ dom-> getElementsByTagName ('person ')-> item (0)-> getElementsByTagName ('name ') -> item (0)-> nodeValue; output: John

Example3:

 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'
'; // Determine whether a node has an attribute if ($ person-> hasAttributes () {echo' has an attribute ';} else {echo' has no attribute ';} echo'
'; // Obtain the id attribute value of the first person echo $ person-> getAttribute ('id ');

In the previous example, only one piece of data can be obtained. now, how can I get complete information from all users?

Example 4:

If This kind of property will also be traversed?

 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'
';}?>

The above introduces the xml dom (10) in php, including some content, and hopes to help those who are interested in PHP tutorials.

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.