XMLDOM (11) and xmldom in php. XMLDOM (11) in php, xmldom7. create a node in dom operation, add, delete, and modify the operation must find the parent node 1, DOMElementDOMDocument: createElement (string $ name [, string $ value] 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.
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
8Delete nodes
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 ');
9Modify 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 ');
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
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.
Invalid DOM (11), xmldom 7. create a node in the dom operation, you must find the parent node 1, DOMElement DOMDocument: createElement (string $ name [, string $ value]...