Xml dom (11) and xmldom in php
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.
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> 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:
<person> <name>huangliu</name> <age>18</age> </person>
8Delete nodes
DOMNode: removeChild (DOMNode $ oldnode)
DOMNode $ oldnode: The Node object to be deleted.
Example: delete the following blue part.
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> 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 ');
9Modify a node
DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode)
DOMNode $ newnode: New Node
DOMNode $ oldnode: original Node
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> 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 ');
10Save
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, modify, and delete 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
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> 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
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> 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
<? Php header ('content-Type: text/html; charset = gb2312 '); $ dom = new DOMDocument ('1. 0', 'utf-8'); $ dom-> 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.