PHP - Xml - Adding or Removing XML Nodes
来源:互联网 发布:win7怎么关闭端口 编辑:程序博客网 时间:2024/06/06 21:03
Use the appendChild(), replaceChild(), and removeChild() methods from PHP's DOM extension:
- <?php
- // define XML data string
- $xmlData = <<< END
- <?xml version="1.0"?>
- <favorites>
- <pet>Humphrey Hippo</pet>
- <flavor>chocolate</flavor>
- <movie>Star Wars</movie>
- </favorites>
- END;
- // read XML data
- $xml = new DOMDocument();
- $xml->formatOutput = true; // format output
- $xml->preserveWhiteSpace = false; // discount whitespace
- $xml->loadXML($xmlData) or die("ERROR: ↩
- Cannot create DOMDocument object");
- // print the original XML tree
- echo "<xmp>OLD:/n" . $xml->saveXML() . "</xmp>";
- // get document element
- $root = $xml->documentElement;
- // add a node before <movie>
- $movie = $root->childNodes->item(2);
- $book = $xml->createElement("book");
- $root->insertBefore($book, $movie);
- $book->appendChild($xml->createTextNode("The Lord Of The Rings"));
- // add a node after <movie>
- $toy = $xml->createElement("toy");
- $toy->appendChild($xml->createTextNode("Stuffed bear"));
- $root->appendChild($toy);
- // replace <flavor> with <icecream> $flavour = $root->childNodes->item(1); $icecream = $xml->createElement("icecream");
- $icecream->appendChild($xml->createTextNode("strawberry")); $root->replaceChild($icecream, $flavour);
- // delete <movie>
- $movie = $root->childNodes->item(3);
- $root->removeChild($movie);
- // print the modified XML tree
- echo "<xmp>NEW:/n" . $xml->saveXML() . "</xmp>";
- ?>
- PHP - Xml - Adding or Removing XML Nodes
- PHP - XML - Filtering XML Nodes by Namespace
- PHP - XML - Filtering XML Nodes with XPath
- Working with XML nodes
- Working with XML nodes
- Adding And Removing Remote Branches
- PHP - XML - Creating XML
- PHP - XML - Validating XML
- PHP - XML - Transforming XML
- Adding and Removing Tags on GitHub
- Git: Adding and Removing Tags on GitHub
- 3.9.2 - Lists - Adding and Removing Objects
- xml php
- PHP XML
- php xml
- PHP---XML
- PHP & XML
- Adding new nodes to GI
- C++代码
- 初学者的迷茫
- PHP - XML - Creating XML
- JS: onfocus和onblur事件应用举例
- JAVA的几个常见错误简析
- PHP - Xml - Adding or Removing XML Nodes
- PSMLIB个人服务器管理组件,IIS管理组件,FTP管理组件
- .NET 2.0 中TextBox只读时在客户端赋值后的取值问题
- Javascript document.all用法
- 股指期货(一)
- 你知道dB的来历吗?
- VC 6.0 下搭建 wxWidgets 开发环境
- Oracle备份与恢复案例
- 赵本山风光嫁女 林肯悍马排队场面奢华