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
Copy codeThe Code is as follows: <? Php
$ Xmlpatch = 'index. xml ';
$ _ Id = '1 ';
$ _ Title = 'title1 ';
$ _ Content = 'content1 ';
$ _ Author = 'author1 ';
$ _ Sendtime = 'time1 ';
$ _ Htmlpatch = '1.html ';
Jb51.net $ doc = new DOMDocument ('1. 0', 'utf-8 ');
$ Doc-> formatOutput = true;
Jb51.net $ root = $ doc-> createElement ('root'); // create a node
Jb51.net $ index = $ doc-> createElement ('index'); // create a node
Jb51.net $ 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.
Jb51.net $ id = $ doc-> createAttribute ('id ');
$ Newsid = $ doc-> createTextNode ($ _ id );
$ Id-> appendChild ($ newsid );
Jb51.net $ title = $ doc-> createAttribute ('title ');
$ Newstitle = $ doc-> createTextNode ($ _ title );
$ Title-> appendChild ($ newstitle );
Jb51.net $ content = $ doc-> createTextNode ($ _ content); // node Value
Jb51.net $ author = $ doc-> createAttribute ('author ');
$ Newsauthor = $ doc-> createTextNode ($ _ author );
$ Author-> appendChild ($ newsauthor );
Jb51.net $ sendtime = $ doc-> createAttribute ('time ');
$ Newssendtime = $ doc-> createTextNode ($ _ sendtime );
$ Sendtime-> appendChild ($ newssendtime );
Jb51.net $ index-> appendChild ($ id); // set $ id to the attribute of the index node.
$ Index-> appendChild ($ title );
$ Index-> appendChild ($ content );
$ Index-> appendChild ($ url );
$ Index-> appendChild ($ author );
$ Index-> appendChild ($ sendtime );
Jb51.net $ root-> appendChild ($ index); // you can specify the index as the root byte.
Jb51.net $ doc-> appendChild ($ root); // set root as the heel Node
Jb51.net $ doc-> save ($ xmlpatch); // save the file
Jb51.netecho $ xmlpatch. 'Has create success ';
Jb51.net?>
Jb51.net <! 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>
Jb51.net <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 NodeCopy codeThe Code is as follows: <? Php
$ Xmlpatch = 'index. xml ';
$ _ Id = '2 ';
$ _ Title = 'title2 ';
$ _ Content = 'content2 ';
$ _ Author = 'author2 ';
$ _ Sendtime = 'time2 ';
$ _ Htmlpatch = '2.html ';
Jb51.net $ doc = new DOMDocument ();
$ Doc-> formatOutput = true;
If ($ doc-> load ($ xmlpatch )){
$ Root = $ doc-> documentElement; // obtain the root node (root)
$ Index = $ doc-> createElement ('index ');
Jb51.net $ url = $ doc-> createAttribute ('url ');
$ Patch = $ doc-> createTextNode ($ _ htmlpatch );
$ Url-> appendChild ($ patch );
Jb51.net $ id = $ doc-> createAttribute ('id ');
$ Newsid = $ doc-> createTextNode ($ _ id );
$ Id-> appendChild ($ newsid );
Jb51.net $ title = $ doc-> createAttribute ('title ');
$ Newstitle = $ doc-> createTextNode ($ _ title );
$ Title-> appendChild ($ newstitle );
Jb51.net $ content = $ doc-> createTextNode ($ _ content );
Jb51.net $ author = $ doc-> createAttribute ('author ');
$ Newsauthor = $ doc-> createTextNode ($ _ author );
$ Author-> appendChild ($ newsauthor );
Jb51.net $ sendtime = $ doc-> createAttribute ('time ');
$ Newssendtime = $ doc-> createTextNode ($ _ sendtime );
$ Sendtime-> appendChild ($ newssendtime );
Jb51.net $ index-> appendChild ($ id );
$ Index-> appendChild ($ title );
$ Index-> appendChild ($ content );
$ Index-> appendChild ($ url );
$ Index-> appendChild ($ author );
$ Index-> appendChild ($ sendtime );
Jb51.net $ root-> appendChild ($ index );
Jb51.net $ doc-> save ($ xmlpatch );
Jb51.netecho $ _ id. 'has been added in '. $ xmlpatch;
Jb51.net} 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>
Jb51.net <body>
</Body>
</Html>
// Edit. php modification function (only the title attribute value and node value are modified here)Copy codeThe Code is as follows: <? Php
$ Xmlpatch = 'index. xml ';
$ _ Id = '2 ';
$ _ Title = 'has been changed ';
$ _ Content = 'has been changed ';
Jb51.net $ doc = new DOMDocument ();
$ Doc-> formatOutput = true;
Jb51.netif ($ 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! ';
}
Jb51.net?>
<! 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>
Jb51.net <body>
</Body>
</Html>
// Delete del. phpCopy codeThe Code is as follows: <? Php
$ Xmlpatch = 'index. xml ';
$ _ Id = '2 ';
Jb51.net $ 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! ';
}
Jb51.net?>
<! 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>
Jb51.net <body>
</Body>
</Html>
Jb51.net
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, first obtain the node list getElementsByTagName and then use foreach to traverse the node to be modified.