Php DOMDocument simple usage example code (XML creation, addition, deletion, modification) _ PHP Tutorial

Source: Internet
Author: User
Php DOMDocument simple usage example code (XML creation, addition, deletion, modification ). There are four files in total: Create, add, delete, and modify. all the variables are written to death. you can use the index to receive them in the $ _ POST method. the copy code of the php creation function is divided into four files: Create, add, delete, and modify. the variables are all written to death. you can use the $ _ POST method to receive the code.
// Index. php creation function

The code is as follows:


$ 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



XML operations

Jb51.net



// Add. php adds a function (similar to the index. php file, mainly adding load to load and $ root = $ doc-> documentElement to obtain and node

The code is as follows:


$ 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! ';
}
?>




XML operation-add

Jb51.net



// Edit. php modification function (only the title attribute value and node value are modified here)

The code is as follows:


$ 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?>




XML operation-modify

Jb51.net



// Delete del. php

The code is as follows:


$ 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?>




XML operation-delete

Jb51.net



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.

Else // index. php creation function...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.