7. Create a Node
In DOM operations, adding and deleting operations must find the parent node
1, DomElement domdocument::createelement (String $name [, String $value])
Creates a node and returns a Node object
String $name: Name of the node (element name)
[, String $value]: The value of the node
2. Domnode domnode::appendchild (Domnode $newnode)
Append child nodes
Domnode $newnode: Child nodes
3, int domdocument::save (string $filename)
Saves data in the DOM to the specified file
4. DocumentElement property, representing the root node in the DOM model
PHP Header ('content-type:text/html;charset=gb2312' ); $dom=NewDOMDocument ('1.0','Utf-8' ); $dom->load ('Demo01.xml' );//Creating a node simply means creating the node object in memory, but this object and our previous//DOM model doesn't have any connection .$person = $dom->createelement (' Person' ); $name= $dom->createelement ('name','Huangliu' ); $age= $dom->createelement (' Age',' -' );//Append name and age as child nodes to the person node$personappendchild ($name); $person-appendchild ($age);//$dom->getelementsbytagname (' persons ')->item (0)->appendchild ($newnode)//Append person to child node to root node$dom->documentelement->appendchild ($person);//Resave all data in the DOM to the Demo01.xml file$dom->save ('Demo01.xml');
Results:
huangliu
8. Deleting nodes
Domnode domnode::removechild (Domnode $oldnode)
Domnode $oldnode: The node object to be deleted
Example: Remove the following blue section
PHP 'content-type:text/html;charset=gb2312' ); New ' 1.0 ' ' Utf-8 ' ); $dom'demo01.xml' ); // first find the Node object to delete ' Person ' 1 ); // Delete a node $dom->documentelement->removechild ($person); // re-save the file ' Demo01.xml ' );
9. Modify the Node
Domnode Domnode::replacechild (Domnode $newnode, Domnode $oldnode)
Domnode $newnode: New node
Domnode $oldnode: Original node
PHP Header ('content-type:text/html;charset=gb2312' ); $dom=NewDOMDocument ('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', - ); $person-appendchild ($name); $person-appendchild ($age);//replace the original node with the new node$dom->documentelement->replacechild ($person, $oldNode);//Save File$dom->save ('Demo01.xml');
10. Save
? String Domdocument::savexml
Saving data in the DOM to a string variable
? int Domdocument::save (string $filename)
Saves data in the DOM to the specified file
11, about the attributes of the deletion and modification
1. Domattr Domelement::setattribute (String $name, String $value)
Add Property
String $name: Property name
String $value: Property value
Add the id= ' s102 ' attribute to person
PHP 'content-type:text/html;charset=gb2312' ); New ' 1.0 ' ' Utf-8 ' ); $dom'demo01.xml' ); // Add id attribute to second person // find a second person ' Person ' 1 ); // Add id attribute ' ID ' ' s102 ' ); // Save File ' Demo01.xml ' );
2. Domattr Domelement::setattribute (String $name, String $value)
modifying properties
String $name: Property name
String $value: Property value
If a node does not have this attribute, the attribute is added (note the difference from the above)
If a node already exists for this property, it indicates that the property is modified
3, bool Domelement::removeattribute (string $name)
Delete attribute (not delete node)
$name: The property to be deleted
PHP 'content-type:text/html;charset=gb2312' ); New ' 1.0 ' ' Utf-8 ' ); $dom'demo01.xml' ); // Delete the id attribute of the 2nd person // find a second person ' Person ' 1 ); // Delete id attribute $person->removeattribute ('ID'); // Save File ' Demo01.xml ' );
4. Get Properties
String Domelement::getattribute (String $name)
String $name: Property name
PHP 'content-type:text/html;charset=gb2312' ); New ' 1.0 ' ' Utf-8 ' ); $dom'demo01.xml' ); // Query the id attribute of a 1th person // find a 1th person. ' Person ' 0 ); // Query ID Property echo $person->getattribute ('ID');
SimpleXML
PHP DOM: Adding and deleting changes
SimpleXML: Query
First, read the XML
1. SimpleXMLElement simplexml_load_file (String $filename)
Reading data from an XML file
String $filename: XML file name
2, Simplexmlelement::__construct (string data)
Reading data from a string
String in data:xml format
If the data is saved to a file, use Simplexml_load_file to read
If the data is saved to a string, the constructor method of the SimpleXMLElement class is used to read
The above describes the XML DOM (11) in PHP, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.