php处理xml

php对xml文件中节点的删除/编辑

使用getElementsByTagName()方法删除/编辑XML节点
三个 文件
1. root.xml
2. remove.php —— 删除节点
3. replace.php —— 替换节点
root.xml
  1. <root>
  2. <child1>child1 content</child1>
  3. <child2>child2 content</child2>
  4. <child3>child3 content</child3>
  5. </root>
remove.php
  1. <?php
  2. $xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串
  3. $doc = DOMDocument::loadXML($xml_str);  //载入这个字符串
  4. //取得元素$child2
  5. $root = $doc->documentElement;
  6. $child2 = $root->getElementsByTagName("child2")->item(0);
  7. //删除$child2对象
  8. $root->removeChild($child2);
  9. //保存文件
  10. $doc->formatOutput = true;
  11. $doc->saveXML();
  12. $doc->save("root.xml");
  13. ?>
replace.php
  1. <?php
  2. $xml_str = file_get_contents("root.xml"); //将root.xml文件中的内容读入一个字符串
  3. $doc = DOMDocument::loadXML($xml_str);  //载入这个字符串
  4. //取得元素$child3
  5. $root = $doc->documentElement;
  6. $child3 = $root->getElementsByTagName("child3")->item(0);
  7. //创建一个newchild元素来替代child3
  8. $root->replaceChild(new DOMElement("newchild", "new content"), $child3);
  9. //保存文件
  10. $doc->formatOutput = true;
  11. $doc->saveXML();
  12. $doc->save("root.xml");
  13. ?>
使用使用XPath删除/修改节点
1. xml.xml
2. re.php —— 替换/删除节点
3. replace.xml —— 替换节点后的XML文件
4. remove.xml —— 删除节点后的XML文件

xml.xml
  1. <?xml version="1.0"?>
  2. <books>
  3. <book>
  4.   <author>Jack Herrington</author>
  5.   <title>PHP Hacks</title>
  6.   <publisher>O'Reilly</publisher>
  7. </book>
  8. <book>
  9.   <author>Jack Herrington</author>
  10.   <title>Podcasting Hacks</title>
  11.   <publisher>O'Reilly</publisher>
  12. </book>
  13. </books>

re.php
  1. <?php
  2. $dom = new DomDocument();
  3. $dom ->load("xml.xml");
  4. $xpath = new domxpath($dom);
  5. //修改节点
  6. $oldtitle = $xpath->query("/books/book/title")->item(0);
  7. $newtitle = $dom->createElement("title");
  8. $newtitle->appendChild(new DOMText("NEW"));
  9. $oldtitle->parentNode->replaceChild($newtitle, $oldtitle);
  10. $dom->save("replace.xml");
  11. //删除节点
  12. $oldtitle = $xpath->query("/books/book/title")->item(1);
  13. $oldtitle->parentNode->removeChild($oldtitle);
  14. $dom->save("remove.xml");
  15. ?>
replace.xml

  1. <?xml version="1.0"?>
  2. <books>
  3. <book>
  4.   <author>Jack Herrington</author>
  5.   <title>NEW</title>
  6.   <publisher>O'Reilly</publisher>
  7. </book>
  8. <book>
  9.   <author>Jack Herrington</author>
  10.   <title>Podcasting Hacks</title>
  11.   <publisher>O'Reilly</publisher>
  12. </book>
  13. </books>
remove.php
  1. <?xml version="1.0"?>
  2. <books>
  3. <book>
  4.   <author>Jack Herrington</author>
  5.   <title>NEW</title>
  6.   <publisher>O'Reilly</publisher>
  7. </book>
  8. <book>
  9.   <author>Jack Herrington</author>
  10.   <publisher>O'Reilly</publisher>
  11. </book>
  12. </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>";
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值