Php xml, delete, modify, and create

Source: Internet
Author: User
Tags php file php tutorial

The four functions are creation, addition, deletion, and modification. The variables are all written to death. You can use the $ _ post method to receive the changes.
// Index. php tutorial creation function
Copy the code as follows:
<? Php
$ Xmlpatch = 'index. XML ';
$ _ Id = '1 ';
$ _ Title = 'title1 ';
$ _ Content = 'content1 ';
$ _ Author = 'author1 ';
$ _ Sendtime = 'time1 ';
$ _ Htmlpatch = '1.html ';
111cn.net $ doc = new domdocument ('1. 0', 'utf-8 ');
$ Doc-> formatoutput = true;
111cn.net $ root = $ doc-> createelement ('root'); // Create a node
111cn.net $ index = $ doc-> createelement ('index'); // Create a node
111cn.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.
111cn.net $ id = $ doc-> createattribute ('id ');
$ Newsid = $ doc-> createtextnode ($ _ id );
$ Id-> appendchild ($ newsid );
111cn.net $ title = $ doc-> createattribute ('title ');
$ Newstitle = $ doc-> createtextnode ($ _ title );
$ Title-> appendchild ($ newstitle );
111cn.net $ content = $ doc-> createtextnode ($ _ content); // node value
111cn.net $ author = $ doc-> createattribute ('author ');
$ Newsauthor = $ doc-> createtextnode ($ _ author );
$ Author-> appendchild ($ newsauthor );
111cn.net $ sendtime = $ doc-> createattribute ('time ');
$ Newssendtime = $ doc-> createtextnode ($ _ sendtime );
$ Sendtime-> appendchild ($ newssendtime );
111cn.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 );
111cn.net $ root-> appendchild ($ index); // you can specify the index as the root byte.
111cn.net $ doc-> appendchild ($ root); // Set root as the heel node
111cn.net $ doc-> save ($ xmlpatch); // save the file
111cn. netecho $ xmlpatch. 'Has create success ';
111cn.net?>
111cn.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>
111cn.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 node
Copy the code as follows:
<? Php
$ Xmlpatch = 'index. XML ';
$ _ Id = '2 ';
$ _ Title = 'title2 ';
$ _ Content = 'content2 ';
$ _ Author = 'author2 ';
$ _ Sendtime = 'time2 ';
$ _ Htmlpatch = '2.html ';
111cn.net $ doc = new domdocument ();
$ Doc-> formatoutput = true;
If ($ doc-> load ($ xmlpatch )){
$ Root = $ doc-> documentelement; // Obtain the root node (root)
$ Index = $ doc-> createelement ('index ');
111cn.net $ url = $ doc-> createattribute ('URL ');
$ Patch = $ doc-> createtextnode ($ _ htmlpatch );
$ Url-> appendchild ($ patch );
111cn.net $ id = $ doc-> createattribute ('id ');
$ Newsid = $ doc-> createtextnode ($ _ id );
$ Id-> appendchild ($ newsid );
111cn.net $ title = $ doc-> createattribute ('title ');
$ Newstitle = $ doc-> createtextnode ($ _ title );
$ Title-> appendchild ($ newstitle );
111cn.net $ content = $ doc-> createtextnode ($ _ content );
111cn.net $ author = $ doc-> createattribute ('author ');
$ Newsauthor = $ doc-> createtextnode ($ _ author );
$ Author-> appendchild ($ newsauthor );
111cn.net $ sendtime = $ doc-> createattribute ('time ');
$ Newssendtime = $ doc-> createtextnode ($ _ sendtime );
$ Sendtime-> appendchild ($ newssendtime );
111cn.net $ index-> appendchild ($ id );
$ Index-> appendchild ($ title );
$ Index-> appendchild ($ content );
$ Index-> appendchild ($ url );
$ Index-> appendchild ($ author );
$ Index-> appendchild ($ sendtime );
111cn.net $ root-> appendchild ($ index );
111cn.net $ doc-> save ($ xmlpatch );
111cn. netecho $ _ id. 'Has been added in'. $ xmlpatch;
111cn.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>
111cn.net <body>
</Body>
</Html>

// Edit. php modification function (only the title attribute value and node value are modified here)
Copy the code as follows:
<? Php
$ Xmlpatch = 'index. XML ';
$ _ Id = '2 ';
$ _ Title = 'has been changed ';
$ _ Content = 'has been changed ';
111cn.net $ doc = new domdocument ();
$ Doc-> formatoutput = true;
111cn. 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! ';
}
111cn.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>
111cn.net <body>
</Body>
</Html>

// Delete del. php
Copy the code as follows:
<? Php
$ Xmlpatch = 'index. XML ';
$ _ Id = '2 ';
111cn.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! ';
}
111cn.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>
111cn.net <body>
</Body>
</Html>

 

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.