XMLDOM (11) in php)

Source: Internet
Author: User
: This article mainly introduces XMLDOM (11) in php. if you are interested in the PHP Tutorial, refer to it. 7. create a node

In dom operations, you must find the parent node for addition, deletion, and modification operations.

1. DOMElement DOMDocument: createElement (string $ name [, string $ value])

Create a node. a node object is returned.

String $ name: name of the node (element name)

[, String $ value]: node value

2. DOMNode: appendChild (DOMNode $ newnode)

Append a subnode

DOMNode $ newnode: subnode

3. int DOMDocument: save (string $ filename)

Save the data in the dom to the specified file.

4. the documentElement attribute indicates the root node in the dom model.

 Load ('demo01. XML'); // Create a node only indicates that the node object is created in the memory, however, this object has no connection with our previous // dom model $ person = $ dom-> createElement ('person '); $ name = $ dom-> createElement ('name', 'huangliu'); $ age = $ dom-> createElement ('age', '18 '); // Append name and age as subnodes to the person node $ person-> appendChild ($ name); $ person-> appendChild ($ age ); // $ dom-> getElementsByTagName ('persons ')-> item (0)-> appendChild ($ newnode) // Append the person subnode to the root node $ dom-> documentElement-> appendChild ($ person ); // save all the data in the dom to the demo01.xml file $ dom-> save ('demo01. XML ');

Result:

         
  
   huangliu
          18    
 

8. delete a node

DOMNode: removeChild (DOMNode $ oldnode)

DOMNode $ oldnode: the node object to be deleted.

Example: delete the following Blue part.

 Load ('demo01. XML'); // first find the node object to be deleted $ person = $ dom-> getElementsByTagName ('person ')-> item (1 ); // delete the node $ dom-> documentElement-> removeChild ($ person); // save the file $ dom-> save ('demo01. XML ');

9. modify a node

DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode)

DOMNode $ newnode: new node

DOMNode $ oldnode: original node

 Load ('demo01. XML'); // find the node to be replaced $ oldNode = $ dom-> getElementsByTagName ('person ')-> item (2 ); // create a new node $ person = $ dom-> createElement ('person '); $ name = $ dom-> createElement ('name', 'songjiang '); $ age = $ dom-> createElement ('age', 100); $ person-> appendChild ($ name); $ person-> appendChild ($ age ); // replace the original node $ dom-> documentElement-> replaceChild ($ person, $ oldNode) with the new node; // save the file $ dom-> save ('demo01. XML ');

10. Save

? String DOMDocument: saveXML

Save the data in the dom to a string variable.

? Int DOMDocument: save (string $ filename)

Save the data in the dom to the specified file.

11. add, delete, and modify attributes

1. DOMAttr DOMElement: setAttribute (string $ name, string $ value)

Add attribute

String $ name: attribute name

String $ value: attribute value

Add the id = 's102 'attribute to person

 Load ('demo01. XML'); // add the id attribute for the second person // find the second person $ person = $ dom-> getElementsByTagName ('person ')-> item (1 ); // add the id attribute $ person-> setAttribute ('id', 's102 '); // save the file $ dom-> save ('demo01. XML ');

2. DOMAttr DOMElement: setAttribute (string $ name, string $ value)

Modify attributes

String $ name: attribute name

String $ value: attribute value

If a node does not have this attribute, it indicates adding the attribute (note the difference with the above)

If a node already has this attribute, it indicates modifying the attribute.

3. bool DOMElement: removeAttribute (string $ name)

Delete attribute (not delete node)

$ Name: attribute to be deleted

 Load ('demo01. XML'); // delete the id attribute of 2nd People // find the second person $ person = $ dom-> getElementsByTagName ('person ')-> item (1 ); // delete the id attribute $ person-> removeAttribute ('id'); // save the file $ dom-> save ('demo01. XML ');

4. get attributes

String DOMElement: getAttribute (string $ name)

String $ name: attribute name

 Load ('demo01. XML'); // query the id attributes of 1st People // find 1st people $ person = $ dom-> getElementsByTagName ('person ')-> item (0 ); // query the id attribute echo $ person-> getAttribute ('id ');

12 SimpleXML

Php dom: add, delete, and modify

SimpleXML: Query

1. read xml

1. simpleXMLElement simplexml_load_file (string $ filename)

Read data from an xml file

String $ filename: xml file name

2. SimpleXMLElement ::__ construct (string data)

Read data from a string

String data: String in xml format

If the data is saved to a file, use simplexml_load_file to read the data.

If the data is saved to a string, use the SimpleXMLElement class constructor to read the data.

The above introduces the xml dom (11) 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.