There are many ways to write XML in PHP. Here we will mainly introduce the usage of domdocument, which is basically the same as that in JS. It is actually very simple.
There are four files in total: Create, add, delete, and modify. the variables are all written to death. You can use the $ _ POST method to receive the changes.
// Index. php creation Function
<? PHP
$ Xmlpatch = 'index. xml ';
$ _ Id = '1 ';
$ _ Title = 'title1 ';
$ _ Content = 'content1 ';
$ _ Author = 'author1 ';
$ _ Sendtime = 'time1 ';
$ _ Htmlpatch = '1.html ';
$ Doc = new domdocument ('1. 0', 'utf-8 ');
$ Doc-> formatoutput = true;
$ Root = $ doc-> createelement ('root'); // create a node
$ Index = $ doc-> createelement ('index'); // create a node
$ Url = $ doc-> createattribute ('url'); // create an attribute
$ Patch = $ doc-> createtextnode ($ _ htmlpatch); // create a text value
$ URL-> appendchild ($ patch); // set the $ patch text to the value of the $ URL attribute.
$ Id = $ doc-> createattribute ('id ');
$ Newsid = $ doc-> createtextnode ($ _ id );
$ Id-> appendchild ($ newsid );
$ Title = $ doc-> createattribute ('title ');
$ Newstitle = $ doc-> createtextnode ($ _ title );
$ Title-> appendchild ($ newstitle );
$ Content = $ doc-> createtextnode ($ _ content); // node Value
$ Author = $ doc-> createattribute ('author ');
$ Newsauthor = $ doc-> createtextnode ($ _ author );
$ Author-> appendchild ($ newsauthor );
$ Sendtime = $ doc-> createattribute ('time ');
$ Newssendtime = $ doc-> createtextnode ($ _ sendtime );
$ Sendtime-> appendchild ($ newssendtime );
$ Index-> appendchild ($ id); // set $ ID as the attribute of the index node.
$ Index-> appendchild ($ title );
$ Index-> appendchild ($ content );
$ Index-> appendchild ($ URL );
$ Index-> appendchild ($ author );
$ Index-> appendchild ($ sendtime );
$ Root-> appendchild ($ index); // you can specify the index as the root byte.
$ Doc-> appendchild ($ root); // you can specify root as the root node.
$ Doc-> Save ($ xmlpatch); // save the file
Echo $ xmlpatch. 'Has create SUCCESS ';
?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> XML operation </title>
</Head>
<Body>
</Body>
</Html>
// Add. php adds a function (similar to the index. php file, mainly adding load to load and $ root = $ doc-> documentelement to obtain and Node
<? PHP
$ Xmlpatch = 'index. xml ';
$ _ Id = '2 ';
$ _ Title = 'title2 ';
$ _ Content = 'content2 ';
$ _ Author = 'author2 ';
$ _ Sendtime = 'time2 ';
$ _ Htmlpatch = '2.html ';
$ Doc = new domdocument ();
$ Doc-> formatoutput = true;
If ($ doc-> load ($ xmlpatch )){
$ Root = $ doc-> documentelement; // obtain the root node (Root)
$ Index = $ doc-> createelement ('index ');
$ Url = $ doc-> createattribute ('url ');
$ Patch = $ doc-> createtextnode ($ _ htmlpatch );
$ URL-> appendchild ($ patch );
$ Id = $ doc-> createattribute ('id ');
$ Newsid = $ doc-> createtextnode ($ _ id );
$ Id-> appendchild ($ newsid );
$ Title = $ doc-> createattribute ('title ');
$ Newstitle = $ doc-> createtextnode ($ _ title );
$ Title-> appendchild ($ newstitle );
$ Content = $ doc-> createtextnode ($ _ content );
$ Author = $ doc-> createattribute ('author ');
$ Newsauthor = $ doc-> createtextnode ($ _ author );
$ Author-> appendchild ($ newsauthor );
$ Sendtime = $ doc-> createattribute ('time ');
$ Newssendtime = $ doc-> createtextnode ($ _ sendtime );
$ Sendtime-> appendchild ($ newssendtime );
$ Index-> appendchild ($ id );
$ Index-> appendchild ($ title );
$ Index-> appendchild ($ content );
$ Index-> appendchild ($ URL );
$ Index-> appendchild ($ author );
$ Index-> appendchild ($ sendtime );
$ Root-> appendchild ($ index );
$ Doc-> Save ($ xmlpatch );
Echo $ _ id. 'has been added in'. $ xmlpatch;
} Else {
Echo 'xml file loaded error! ';
}
?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> XML operation-add </title>
</Head>
<Body>
</Body>
</Html>
// Edit. php modification function (only the title attribute value and node value are modified here)
<? PHP
$ Xmlpatch = 'index. xml ';
$ _ Id = '2 ';
$ _ Title = 'has been changed ';
$ _ Content = 'has been changed ';
$ Doc = new domdocument ();
$ Doc-> formatoutput = true;
If ($ doc-> load ($ xmlpatch )){
$ Root = $ doc-> documentelement;
$ Elm = $ root-> getelementsbytagname ('index ');
$ Checkexist = 0;
Foreach ($ elm as $ new ){
If ($ New-> getattribute ('id') =$ _ id ){
$ New-> setattribute ('title', $ _ title );
$ New-> nodevalue = $ _ content; // It's too surprising to modify the node value. I didn't expect to assign values directly like JS...
// $ New-> removechild ($ New-> nodevalue );
$ Checkexist = 1;
}
}
If ($ checkexist = 0 ){
Echo $ _ id. 'is not found in'. $ xmlpatch;
} Else {
$ Doc-> Save ($ xmlpatch );
Echo $ _ id. 'has been changed ';
}
} Else {
Echo 'xml file loaded error! ';
}
?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> XML operation-Modify </title>
</Head>
<Body>
</Body>
</Html>
// Delete del. php
<? PHP
$ Xmlpatch = 'index. xml ';
$ _ Id = '2 ';
$ Doc = new domdocument ();
$ Doc-> formatoutput = true;
If ($ doc-> load ($ xmlpatch )){
$ Root = $ doc-> documentelement;
$ Elm = $ root-> getelementsbytagname ('index ');
Foreach ($ elm as $ new ){
If ($ New-> getattribute ('id') =$ _ id ){
If ($ root-> removechild ($ new )){
Echo $ _ id. 'has been deleted ';
} Else {
Echo $ _ id. 'delete failed ';
}
}
}
$ Doc-> Save ($ xmlpatch );
} Else {
Echo 'xml file loaded error! ';
}
?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> XML operation-delete </title>
</Head>
<Body>
</Body>
</Html>
To sum up, "CREATE" and "add" are mainly used for creation and appendchild. "CREATE" is followed by "element", "attribute" is used to create attributes, textnode is used to create values, and "appendchild" is used to set subordination, this is very simple.
To delete or modify a node, use getelementsbytagname to obtain the node list first, and then use foreach to traverse the node to be modified.
From: http://hi.baidu.com/chickenlove/blog/item/e960b34f4450253bafc3abd7.html