Detailed explanation of PHP native DOM object operation XML method, detailed explanation of phpdomxml

Source: Internet
Author: User
Tags baseuri

Detailed explanation of PHP native DOM object operation XML method, detailed explanation of phpdomxml

I. 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.

<?xml version="1.0"?><EventList> <event obj="power" info="power is shutdown">PHP0</event> <event obj="memcache" info="memcache used than 90%">PHP1</event> <event obj="cpu" info="cpu used than 95%">PHP2</event> <event obj="disk" info="disk is removed">PHP3</event></EventList>

Ii. 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 "<pre> Object: $ attr_obj; Info: $ attr_info; Value: {$ item-> nodeValue} </pre>"; $ 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.

<?xml version="1.0"?><EventList> <event obj="power" info="power is shutdown" count="1">PHP0</event> <event obj="memcache" info="memcache used than 90%" count="1">PHP1</event> <event obj="cpu" info="cpu used than 95%" count="1">PHP2</event> <event obj="disk" info="disk is removed" count="1">PHP3</event></EventList>

3. 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. The new value iscount+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 t. xml file after the operation is as follows:obj=cpuThe node's count attribute has been changed, and the value has been modified successfully.

<?xml version="1.0"?><EventList> <event obj="power" info="power is shutdown" count="1">PHP0</event> <event obj="memcache" info="memcache used than 90%" count="1">PHP1</event> <event obj="cpu" info="cpu used than 95%" count="2">Hello,Kitty</event> <event obj="disk" info="disk is removed" count="1">PHP3</event></EventList>

4. delete a node

To be added, it will be deleted. The t. xml file in the preceding section is an operation object. Deleteobj=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.

<?xml version="1.0"?><EventList> <event obj="power" info="power is shutdown" count="1">PHP0</event> <event obj="memcache" info="memcache used than 90%" count="1">PHP1</event> <event obj="cpu" info="cpu used than 95%" count="2">Hello,Kitty</event> </EventList>

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.

<?xml version="1.0"?><EventList> <event obj="power" info="power is shutdown" count="1">PHP0</event> <event obj="memcache" info="memcache used than 90%" count="1">PHP1</event> <event obj="cpu" info="cpu used than 95%" count="2">Hello,Kitty</event> <event obj="lenovo" info="thinkpad t430">lenovo</event></EventList>

5. About item ($ index)

item(index)Is a method in the DOMNodeList class. It returns a node specified by the index. In the DOMDocument classgetElementsByTagName(name)The method returns an instance of the DOMNodeList object, so you can directly callitem(index)Method. T. xml in the preceding section is used as an example. Ife=dom−>getElementsByTagName(‘EventList′)Obtain the information of the EventList node. Because the EventList node is the root node and has only one node, when it calls item (index), the index is only available when index = 0, because it only has one; ife=dom−>getElementsByTagName(‘event′)Obtains the information of the event node. Because there are four events, it callsitem(index)Hour, Index$index={0,1,2,3} . You can select four values. 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 ;//}

Summary

The above is all the content of this article. I hope the content of this article will be helpful for everyone to learn or use PHP. If you have any questions, please leave a message.

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.