PHP native DOM object operation XML

Source: Internet
Author: User
Tags baseuri
For operating XML files, PHP has a built-in set of DOM objects for processing. For XML operations, functions in the DOM object can be used from creation, addition, modification, and deletion. For operating XML files, PHP has a built-in set of DOM objects for processing. For XML operations, functions in the DOM object can be used from creation, addition, modification, and deletion.

Create

Create a new XML file and write some data to it.

/** Create an xml file */$ info = array ('obj '=> 'power', 'info' => 'Power is shutdown '), array ('obj '=> 'memcache', 'info' => 'memcache used than 90%'), array ('obj '=> 'CPU ', 'info' => 'CPU used than 95% '), array ('obj' => 'disk', 'info' => 'disk is removed ')); // the data to be written $ dom = new DOMDocument ('1. 0 '); $ dom-> formatOutput = true; // format $ eventList = $ dom-> createElement ('eventlist '); // create the root node EventList $ dom-> appendChild ($ eventList); // add the root node for ($ I = 0; $ I <count ($ info ); $ I ++) {$ event = $ dom-> createElement ('event'); // Create a node event $ text = $ dom-> createTextNode ('php '. $ I); // create a text node with the value PHP0, PHP1... $ event-> appendChild ($ text); // add a text node to the node event as the value of the node event $ attr_obj = $ dom-> createAttribute ('obj '); // create the property obj $ attr_obj-> value = $ info [$ I] ['obj ']; // assign a value to the obj property $ event-> appendChild ($ attr_obj ); // add the obj attribute to the event node as the attribute of the event node $ attr_info = $ dom-> createAttribute ('info '); $ attr_info-> value = $ info [$ I] ['info']; $ event-> appendChild ($ attr_info); $ eventList-> appendChild ($ event ); // add the event node to the root node EventList} // echo $ dom-> saveXML (); $ dom-> save ('. /t. XML'); // save the information to the t. xml file

The code snippet above can create an XML file and add some information to the file, including the values and attributes. The final file is t in the current directory. xml, you can look at its content.

 
   
  
   PHP0
    
  
   PHP1
    
  
   PHP2
    
  
   PHP3
  
 

Read XML Information & add new attributes


The t. xml file created in the preceding section is an operation object. read the information in the t. xml file and add a new attribute count to the node. The value is 1.

/** Read the xml file information and add a new property */$ dom = new DOMDocument ('1. 0 '); $ dom-> load ('. /t. XML'); // load the file to be operated $ list = $ dom-> getElementsByTagName ('event'); // Obtain the event node list foreach ($ list as $ item) {$ attr_obj = $ item-> getAttribute ('obj '); // get the value of the property obj $ attr_info = $ item-> getAttribute ('info'); echo"
Object:$attr_obj;Info:$attr_info;Value:{$item->nodeValue}
"; $ Item-> setAttribute ('count', 1); // add the new property count = 1} $ dom-> save ('. /t. XML'); // Save the modification

Take a look at the extracted value:

Object:power;Info:power is shutdown;Value:PHP0 Object:memcache;Info:memcache used than 90%;Value:PHP1 Object:cpu;Info:cpu used than 95%;Value:PHP2 Object:disk;Info:disk is removed;Value:PHP3

Let's take a look at the contents of the current t. xml file. the count attribute has been added.

 
   
  
   PHP0
    
  
   PHP1
    
  
   PHP2
    
  
   PHP3
  
 

Modify node attributes and node values

The t. xml file in the preceding section is the operation object. modify the obj attribute to the count value of the cpu node, and the new value is count + 1.

/** Modify the attributes and values of a node */$ dom = new DOMDocument ('1. 0 '); $ dom-> load ('. /t. xml '); $ list = $ dom-> getElementsByTagName ('event'); foreach ($ list as $ item) {$ attr_obj = $ item-> getAttribute ('obj '); if ($ attr_obj = 'CPU') {// modify the count attribute of the cpu, make the value + 1 $ attr_count = $ item-> getAttribute ('count'); // obtain the value of the count attribute $ item-> setAttribute ('count ', $ attr_count + 1); // reset the value of the count attribute $ item-> nodeValue = 'Hello, Kitty '; // reset the node value }}$ dom-> save ('. /t. XML ');

The operated t. xml file is as follows. you can see that the count attribute of the node with obj = cpu has been changed and the value has been modified successfully.

 
   
  
   PHP0
    
  
   PHP1
    
  
   Hello,Kitty
    
  
   PHP3
  
 

Delete a node

To be added, it will be deleted. The t. xml file in the preceding section is an operation object. delete the node with obj = disk.

/** Delete node */$ dom = new DOMDocument ('1. 0 '); $ dom-> load ('. /t. xml '); $ list = $ dom-> getElementsByTagName ('event'); foreach ($ list as $ item) {if ($ item-> getAttribute ('obj ') = 'disk') {// take the obj = disk node as the operation object $ item-> parentNode-> removeChild ($ item ); // delete node }}$ dom-> save ('. /t. XML ');

Check the content of the t. xml file after the operation. The obj = disk node has been deleted successfully.

 
   
  
   PHP0
    
  
   PHP1
    
  
   Hello,Kitty
   
 

Add a new subnode to the root node


T. xml in the preceding section is the operation object. add a new subnode to the root node EventList.

/** Add a subnode to EventList */$ dom = new DOMDocument ('1. 0 '); $ dom-> load ('. /t. xml '); $ event_list = $ dom-> getElementsByTagName ('eventlist'); // get the root node $ event = $ dom-> createElement ('event ', 'Lenovo '); // Create a node $ event_list-> item (0)-> appendChild ($ event ); // add the new node to the root node $ event_attr_obj = $ dom-> createAttribute ('obj '); $ event_attr_obj-> value = 'Lenovo '; $ event-> appendChild ($ event_attr_obj); $ event_attr_info = $ dom-> createAttribute ('info'); $ event_attr_info-> value = 'thinkpad t430 '; $ event-> appendChild ($ event_attr_info); $ dom-> save ('. /t. XML ');

Take a look at the content of the t. xml file after the operation. The new child node has been inserted into the root node.

 
   
  
   PHP0
    
  
   PHP1
    
  
   Hello,Kitty
   
  
   lenovo
  
 

About item ($ index)

Item (index) is a method in the DOMNodeList class. It returns a node specified by the index. The getElementsByTagName (name) method in the DOMDocument class returns an instance of the DOMNodeList object, so you can directly call the item (index) method. T. xml is used as an example. if e = dom −> getElementsByTagName ('eventlist') is used to obtain the information of the EventList node, because the EventList node is the root node and has only one, so when it calls item (index), the index is only available with index = 0, because it only has one; and if e = dom −> getElementsByTagName ('event ′) obtain the event node information. because there are four events, when it calls item (index), the index $ index = {0, 1, 2, 3} has four values to choose from. Each node contains multiple attributes, which can be expressed in an array of key-value pairs, as shown below:

object(DOMElement)#3 (18) {  ["tagName"]=>  string(5) "event"  ["schemaTypeInfo"]=>  NULL  ["nodeName"]=>  string(5) "event"  ["nodeValue"]=>  string(11) "Hello,Kitty"  ["nodeType"]=>  int(1)  ["parentNode"]=>  string(22) "(object value omitted)"  ["childNodes"]=>  string(22) "(object value omitted)"  ["firstChild"]=>  string(22) "(object value omitted)"  ["lastChild"]=>  string(22) "(object value omitted)"  ["previousSibling"]=>  string(22) "(object value omitted)"  ["nextSibling"]=>  string(22) "(object value omitted)"  ["attributes"]=>  string(22) "(object value omitted)"  ["ownerDocument"]=>  string(22) "(object value omitted)"  ["namespaceURI"]=>  NULL  ["prefix"]=>  string(0) ""  ["localName"]=>  string(5) "event"  ["baseURI"]=>  string(36) "file:/H:/xampp/htdocs/demo/xml/t.xml"  ["textContent"]=>  string(11) "Hello,Kitty"}

It can also be used as an object property, for example, getting the value of this node:

/** About item () */$ dom = new DOMDocument ('1. 0 '); $ dom-> load ('. /t. xml '); $ e = $ dom-> getElementsByTagName ('event'); echo $ e-> item (2)-> nodeValue; // var_dump ($ e-> item (2); // $ e = $ dom-> getElementsByTagName ('eventlist '); // var_dump ($ e-> item (0); // var_dump ($ e-> item (0)-> baseURI); // for ($ I = 0; $ I <$ e-> length; $ I ++) {// echo $ e-> item ($ I)-> nodeValue ;//}

The above is the XML content for PHP native DOM object operations. For more information, see PHP Chinese website (www.php1.cn )!

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.