php对xml文件中节点的删除/编辑
使用getElementsByTagName()方法删除/编辑XML节点
三个
文件:
1.
root.xml
2.
remove.php —— 删除节点
3.
replace.php —— 替换节点
root.xml
- <root>
- <child1>child1 content</child1>
- <child2>child2 content</child2>
- <child3>child3 content</child3>
- </root>
<root>
<child1>child1 content</child1>
<child2>child2 content</child2>
<child3>child3 content</child3>
</root>
remove.php
- <?php
- $xml_str = file_get_contents("root.xml");
- $doc = DOMDocument::loadXML($xml_str);
- $root = $doc->documentElement;
- $child2 = $root->getElementsByTagName("child2")->item(0);
- $root->removeChild($child2);
- $doc->formatOutput = true;
- $doc->saveXML();
- $doc->save("root.xml");
- ?>
<?php
$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串
$doc = DOMDocument::loadXML($xml_str); //载入这个字符串
//取得元素$child2
$root = $doc->documentElement;
$child2 = $root->getElementsByTagName("child2")->item(0);
//删除$child2对象
$root->removeChild($child2);
//保存文件
$doc->formatOutput = true;
$doc->saveXML();
$doc->save("root.xml");
?>
replace.php
- <?php
- $xml_str = file_get_contents("root.xml");
- $doc = DOMDocument::loadXML($xml_str);
- $root = $doc->documentElement;
- $child3 = $root->getElementsByTagName("child3")->item(0);
- $root->replaceChild(new DOMElement("newchild", "new content"), $child3);
- $doc->formatOutput = true;
- $doc->saveXML();
- $doc->save("root.xml");
- ?>
<?php
$xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串
$doc = DOMDocument::loadXML($xml_str); //载入这个字符串
//取得元素$child3
$root = $doc->documentElement;
$child3 = $root->getElementsByTagName("child3")->item(0);
//创建一个newchild元素来替代child3
$root->replaceChild(new DOMElement("newchild", "new content"), $child3);
//保存文件
$doc->formatOutput = true;
$doc->saveXML();
$doc->save("root.xml");
?>
使用使用XPath删除/修改节点
1.
xml.xml
2.
re.php —— 替换/删除节点
3.
replace.xml —— 替换节点后的XML文件
4.
remove.xml —— 删除节点后的XML文件
xml.xml
- <?xml version="1.0"?>
- <books>
- <book>
- <author>Jack Herrington</author>
- <title>PHP Hacks</title>
- <publisher>O'Reilly</publisher>
- </book>
- <book>
- <author>Jack Herrington</author>
- <title>Podcasting Hacks</title>
- <publisher>O'Reilly</publisher>
- </book>
- </books>
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
re.php
- <?php
- $dom = new DomDocument();
- $dom ->load("xml.xml");
- $xpath = new domxpath($dom);
- $oldtitle = $xpath->query("/books/book/title")->item(0);
- $newtitle = $dom->createElement("title");
- $newtitle->appendChild(new DOMText("NEW"));
- $oldtitle->parentNode->replaceChild($newtitle, $oldtitle);
- $dom->save("replace.xml");
- $oldtitle = $xpath->query("/books/book/title")->item(1);
- $oldtitle->parentNode->removeChild($oldtitle);
- $dom->save("remove.xml");
- ?>
<?php
$dom = new DomDocument();
$dom ->load("xml.xml");
$xpath = new domxpath($dom);
//修改节点
$oldtitle = $xpath->query("/books/book/title")->item(0);
$newtitle = $dom->createElement("title");
$newtitle->appendChild(new DOMText("NEW"));
$oldtitle->parentNode->replaceChild($newtitle, $oldtitle);
$dom->save("replace.xml");
//删除节点
$oldtitle = $xpath->query("/books/book/title")->item(1);
$oldtitle->parentNode->removeChild($oldtitle);
$dom->save("remove.xml");
?>
replace.xml
- <?xml version="1.0"?>
- <books>
- <book>
- <author>Jack Herrington</author>
- <title>NEW</title>
- <publisher>O'Reilly</publisher>
- </book>
- <book>
- <author>Jack Herrington</author>
- <title>Podcasting Hacks</title>
- <publisher>O'Reilly</publisher>
- </book>
- </books>
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<title>NEW</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
remove.php
- <?xml version="1.0"?>
- <books>
- <book>
- <author>Jack Herrington</author>
- <title>NEW</title>
- <publisher>O'Reilly</publisher>
- </book>
- <book>
- <author>Jack Herrington</author>
- <publisher>O'Reilly</publisher>
- </book>
- </books>
我自己实际实现时的一些源文件,记录在这,备忘,实现的是对书架的增删改 管理功能
①添加书籍或者杂志
<?php
$title=$_POST[addtitle];
$author=$_POST[addauthor];
$publisher=$_POST[addpublisher];
$time=$_POST[addtime];
$description=$_POST[adddescription];
$price=$_POST[addprice];
$location=$_POST[addlocation];
if($title==="" || $author==="" || $location==="")
{
echo "<font color=/"red/">you should at least input title,author and location</font><br>";
}
else
{
$type=$_POST[radio];
$dom = new DomDocument();
$dom ->load("mybookshelf.xml");
$xpath = new domxpath($dom);
//修改节点
//$olditem=$xpath->query("bookshelf/".$type)->item($mid);
if($type==="book")
{
$shelf = $xpath->query("/bookshelf")->item(0);
$newbook = $dom->createElement("book");
$newtitle= $dom->createElement("title");
$newtitle->appendChild(new DOMText($title));
$newauthor=$dom->createElement("author");
$newauthor->appendChild(new DOMText($author));
$newpublisher=$dom->createElement("publisher");
$newpublisher->appendChild(new DOMText($publisher));
$newtime=$dom->createElement("publish_date");
$newtime->appendChild(new DOMText($time));
$newdescription=$dom->createElement("description");
$newdescription->appendChild(new DOMText($description));
$newprice=$dom->createElement("price");
$newprice->appendChild(new DOMText($price));
$newlocation=$dom->createElement("location");
$newlocation->appendChild(new DOMText($location));
$newbook->appendChild($newtitle);
$newbook->appendChild($newauthor);
$newbook->appendChild($newpublisher);
$newbook->appendChild($newtime);
$newbook->appendChild($newdescription);
$newbook->appendChild($newprice);
$newbook->appendChild($newlocation);
$shelf->appendChild($newbook);
echo "book added!";
}
if($type==="article")
{
$shelf = $xpath->query("/bookshelf")->item(0);
$newarticle = $dom->createElement("article");
$newtitle=$dom->createElement("title");
$newtitle->appendChild(new DOMText($title));
$newauthor=$dom->createElement("author");
$newauthor->appendChild(new DOMText($author));
$newpublisher=$dom->createElement("journal");
$newpublisher->appendChild(new DOMText($publisher));
$newtime=$dom->createElement("year");
$newtime->appendChild(new DOMText($time));
$newdescription=$dom->createElement("description");
$newdescription->appendChild(new DOMText($description));
$newprice=$dom->createElement("price");
$newprice->appendChild(new DOMText($price));
$newlocation=$dom->createElement("location");
$newlocation->appendChild(new DOMText($location));
$newarticle->appendChild($newtitle);
$newarticle->appendChild($newauthor);
$newarticle->appendChild($newpublisher);
$newarticle->appendChild($newtime);
$newarticle->appendChild($newdescription);
$newarticle->appendChild($newprice);
$newarticle->appendChild($newlocation);
$shelf->appendChild($newarticle);
echo "article added!";
}
$dom->save("mybookshelf.xml");
echo "<h3>ok!</h3>";
}
?>
②删除书籍或者杂志
<?php
$rmid=$_GET[rmid];
if(preg_match("//d+/",$rmid)===0)
{
echo "<font color=/"red/">invalid input!</font>";
}
else
{
$type=$_GET[radio];
$xml_str = file_get_contents("mybookshelf.xml");
//echo $xml_str;
$doc = DOMDocument::loadXML($xml_str);
//print_r($doc);
$root = $doc->documentElement;
$item = $root->getElementsByTagName($type)->item((int)$rmid-1);
//echo $item;
$root->removeChild($item);
$doc->formatOutput = true;
$doc->saveXML();
$doc->save("mybookshelf.xml");
echo "<h3>ok</h3>";
}
?>
③修改书籍或者杂志的信息
<?php
$mid=$_POST[mid];
if(preg_match("//d+/",$mid)===0)
{
echo "<font color=/"red/">invalid input!</font><br>";
}
else
{
$type=$_POST[radio];
$mtitle=$_POST[mtitle];
$mauthor=$_POST[mauthor];
$mpublisher=$_POST[mpublisher];
$mtime=$_POST[mtime];
$mdescription=$_POST[mdescription];
$mprice=$_POST[mprice];
$mlocation=$_POST[mlocation];
$dom = new DomDocument();
$dom ->load("mybookshelf.xml");
$xpath = new domxpath($dom);
//修改节点
//$olditem=$xpath->query("bookshelf/".$type)->item($mid);
if($type==="book")
{
if($mtitle!=="")
{
$oldtitle = $xpath->query("/bookshelf/book/title")->item($mid-1);
$newtitle = $dom->createElement("title");
$newtitle->appendChild(new DOMText($mtitle));
$oldtitle->parentNode->replaceChild($newtitle, $oldtitle);
}
if($mauthor!=="")
{
$oldauthor = $xpath->query("/bookshelf/book/author")->item($mid-1);
$newauthor = $dom->createElement("author");
$newauthor->appendChild(new DOMText($mauthor));
$oldauthor->parentNode->replaceChild($newauthor, $oldauthor);
}
if($mpublisher!=="")
{
$oldpublisher = $xpath->query("/bookshelf/book/publisher")->item($mid-1);
$newpublisher = $dom->createElement("publisher");
$newpublisher->appendChild(new DOMText($mpublisher));
$oldpublisher->parentNode->replaceChild($newpublisher, $oldpublisher);
}
if($mtime!=="")
{
$oldtime = $xpath->query("/bookshelf/book/publish_date")->item($mid-1);
$newtime = $dom->createElement("publish_date");
$newtime->appendChild(new DOMText($mtime));
$oldtime->parentNode->replaceChild($newtime, $oldtime);
}
if($mdescription!=="")
{
$olddescription = $xpath->query("/bookshelf/book/description")->item($mid-1);
$newdescription = $dom->createElement("description");
$newdescription->appendChild(new DOMText($mdescription));
$olddescription->parentNode->replaceChild($newdescription, $olddescription);
}
if($mprice!=="")
{
$oldprice = $xpath->query("/bookshelf/book/price")->item($mid-1);
$newprice = $dom->createElement("price");
$newprice->appendChild(new DOMText($mprice));
$oldprice->parentNode->replaceChild($newprice, $oldprice);
}
if($mlocation!=="")
{
$oldlocation = $xpath->query("/bookshelf/book/location")->item($mid-1);
$newlocation = $dom->createElement("location");
$newlocation->appendChild(new DOMText($mlocation));
$oldlocation->parentNode->replaceChild($newlocation, $oldlocation);
}
}
if($type==="article")
{
if($mtitle!=="")
{
$oldtitle = $xpath->query("/bookshelf/article/title")->item($mid-1);
$newtitle = $dom->createElement("title");
$newtitle->appendChild(new DOMText($mtitle));
$oldtitle->parentNode->replaceChild($newtitle, $oldtitle);
}
if($mauthor!=="")
{
$oldauthor = $xpath->query("/bookshelf/article/author")->item($mid-1);
$newauthor = $dom->createElement("author");
$newauthor->appendChild(new DOMText($mauthor));
$oldauthor->parentNode->replaceChild($newauthor, $oldauthor);
}
if($mpublisher!=="")
{
$oldpublisher = $xpath->query("/bookshelf/article/journal")->item($mid-1);
$newpublisher = $dom->createElement("journal");
$newpublisher->appendChild(new DOMText($mpublisher));
$oldpublisher->parentNode->replaceChild($newpublisher, $oldpublisher);
}
if($mtime!=="")
{
$oldtime = $xpath->query("/bookshelf/article/year")->item($mid-1);
$newtime = $dom->createElement("year");
$newtime->appendChild(new DOMText($mtime));
$oldtime->parentNode->replaceChild($newtime, $oldtime);
}
if($mdescription!=="")
{
$olddescription = $xpath->query("/bookshelf/article/description")->item($mid-1);
$newdescription = $dom->createElement("description");
$newdescription->appendChild(new DOMText($mdescription));
$olddescription->parentNode->replaceChild($newdescription, $olddescription);
}
if($mprice!=="")
{
$oldprice = $xpath->query("/bookshelf/article/price")->item($mid-1);
$newprice = $dom->createElement("price");
$newprice->appendChild(new DOMText($mprice));
$oldprice->parentNode->replaceChild($newprice, $oldprice);
}
if($mlocation!=="")
{
$oldlocation = $xpath->query("/bookshelf/article/location")->item($mid-1);
$newlocation = $dom->createElement("location");
$newlocation->appendChild(new DOMText($mlocation));
$oldlocation->parentNode->replaceChild($newlocation, $oldlocation);
}
}
$dom->save("mybookshelf.xml");
echo "<h3>ok!</h3>";
}
?>