A total of four files, is to create, add, delete, modify four functions, variables are written dead, change to a $_post way to receive the use of
index.php Create feature
Copy Code code 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 ');//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 the $patch text to the value of the $url property
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 properties of the index node, the following are similar
$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 Node
Jb51.net$doc-> Save ($xmlpatch);//Saving 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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>xml Operations </title>
Jb51.net<body>
</body>
add.php add functionality (similar to index.php file, mainly add a load load and $root = $doc-> documentelement get with node
Copy Code code 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;//get 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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>xml Operations-Add </title>
Jb51.net<body>
</body>
edit.php Modify function (only the title attribute value and node value are modified here)
Copy Code code 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;//Modify the node value, it is very unexpected, did not expect to be as direct as JS can be assigned 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?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>xml Operations-Modify </title>
Jb51.net<body>
</body>
del.php Delete Feature
Copy Code code 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 ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>xml Operations-Delete </title>
Jb51.net<body>
</body>
Jb51.net
To sum up, create and add the main use is create and appendchild,create behind and element is the creation of nodes, with attribute is to create a property, Textnode is to create a value, and then AppendChild is to set the dependencies , it's very simple to look at. Both deletions and modifications are made by first getting the node list getElementsByTagName and then foreach traversing the node that you want to modify.