DOMDocument Simple Usage Sample code in PHP (XML creation, addition, deletion, modification) _php tutorial

Source: Internet
Author: User
Tags save file
A total of four files, respectively, is to create, add, delete, modify four functions, variables are written dead, change to a $_post way to receive it can be used
index.php Creating features
Copy CodeThe 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 ');//New node
Jb51.net$index = $doc-createelement (' index ');//New node
Jb51.net$url = $doc-createattribute (' url ');//new property
$patch = $doc createTextNode ($_htmlpatch);//New text value
$url-AppendChild ($patch);//Set $patch text to the value of the.
Jb51.net$id = $doc-CreateAttribute (' id ');
$newsid = createTextNode ($_id), $doc;
$id-AppendChild ($NEWSID);
Jb51.net$title = $doc-CreateAttribute (' title ');
$newstitle = createTextNode ($_title), $doc;
$title-AppendChild ($newstitle);
Jb51.net$content = $doc-createTextNode ($_content);//Node value
Jb51.net$author = $doc-CreateAttribute (' author ');
$newsauthor = createTextNode ($_author), $doc;
$author-AppendChild ($newsauthor);
Jb51.net$sendtime = $doc-CreateAttribute (' time ');
$newssendtime = createTextNode ($_sendtime), $doc;
$sendtime-AppendChild ($newssendtime);
Jb51.net$index-AppendChild ($id);//Set $id as the property of the index node, as follows
$index-AppendChild ($title);
$index-AppendChild ($content);
$index-AppendChild ($url);
$index-AppendChild ($author);
$index-AppendChild ($sendtime);
Jb51.net$root, AppendChild ($index);//Set Index to root byte point
Jb51.net$doc-AppendChild ($root);//set Root to follow the node
Jb51.net$doc, Save ($xmlpatch);//Save File
Jb51.netecho $xmlpatch. ' has create success ';
Jb51.net?>
Jb51.net



<title>XML operations</title>

Jb51.net



add.php Add function (similar to the index.php file, the main thing is to add a load and $root = $doc, and documentelement to get the node
Copy CodeThe 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;//Get root node (root)
$index = createelement (' index '), $doc
Jb51.net$url = $doc-createattribute (' url ');
$patch = createTextNode ($_htmlpatch), $doc;
$url-AppendChild ($patch);
Jb51.net$id = $doc-CreateAttribute (' id ');
$newsid = createTextNode ($_id), $doc;
$id-AppendChild ($NEWSID);
Jb51.net$title = $doc-CreateAttribute (' title ');
$newstitle = createTextNode ($_title), $doc;
$title-AppendChild ($newstitle);
Jb51.net$content = $doc-createTextNode ($_content);
Jb51.net$author = $doc-CreateAttribute (' author ');
$newsauthor = createTextNode ($_author), $doc;
$author-AppendChild ($newsauthor);
Jb51.net$sendtime = $doc-CreateAttribute (' time ');
$newssendtime = createTextNode ($_sendtime), $doc;
$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. ' have been added in '. $xmlpatch;
Jb51.net} else {
Echo ' XML file loaded error! ';
}
?>




<title>XML Operations-Add</title>

Jb51.net



edit.php Modify function (only the Title property value and the node value are modified here)
Copy CodeThe 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 = DocumentElement, $doc;
$elm = getElementsByTagName (' index '), $root
$checkexist = 0;
foreach ($elm as $new) {
if ($new-getattribute (' id ') = = $_id) {
$new-SetAttribute (' title ', $_title);
$new-NodeValue = $_content;//Modify the value of the node, it is very unexpected, I did not expect to be as direct as JS can assign value ...
$new, RemoveChild ($new, nodevalue);
$checkexist = 1;
}
}
if ($checkexist = = 0) {
Echo $_id. ' isn't found in '. $xmlpatch;
} else {
$doc, Save ($xmlpatch);
Echo $_id. ' has been changed ';
}
} else {
Echo ' XML file loaded error! ';
}
Jb51.net?>




<title>XML Operations-Modify</title>

Jb51.net



del.php Delete function
Copy CodeThe code is as follows:
$xmlpatch = ' index.xml ';
$_id = ' 2 ';
Jb51.net$doc = new DOMDocument ();
$doc-formatoutput = true;
if ($doc-load ($xmlpatch)) {
$root = DocumentElement, $doc;
$elm = getElementsByTagName (' index '), $root
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?>




<title>XML Operations-Delete</title>

Jb51.net



Jb51.net
To summarize, create and add the main use is the Create and appendchild,create behind the element is the creation of the node, and attribute is to create the property, Textnode is to create the value, and then AppendChild is to set the dependency , so easy to see. Both delete and modify are obtained by first getting the node list getElementsByTagName and then foreach traversing the node that you want to modify.

http://www.bkjia.com/PHPjc/322777.html www.bkjia.com true http://www.bkjia.com/PHPjc/322777.html techarticle A total of four files, respectively, is to create, add, delete, modify four functions, the variables are written dead, change to a $_post way to receive can use the//index.php to create the function copy code ...

  • 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.