PHP - XML - Creating XML
2008-10-11 18:38
169 查看
Use PHP's DOM extension to create a DOM tree and append nodes to it:
<?php
// initialize DOM object
$xml = new DOMDocument("1.0");
// add root node <listing>
$root = $xml->createElement("list");
$xml->appendChild($root);
// add element <description> to root
$desc = $xml->createElement("description");
$root->appendChild($desc);
// add <description> content
$desc->appendChild($xml->createTextNode("Sam's Shopping List"));
// add comment
$root->appendChild($xml->createComment("item list follows"));
// add <item> child element
// add quantities as attributes
$items = $xml->createElement("items");
$root->appendChild($items);
$item = $xml->createElement("item");
$items->appendChild($item);
$item->appendChild($xml->createTextNode("eggs"));
$item->setAttribute("units", 3);
unset($item);
$item = $xml->createElement("item");
$items->appendChild($item);
$item->appendChild($xml->createTextNode("salt"));
$item->setAttribute("units", "100 gm");
// add CDATA block
$items->appendChild($xml->createCDATASection("You can't make↩
an omelette without breaking eggs"));
// add PI
$root->appendChild($xml->createProcessingInstruction(↩
"xml-dummy-pi", "shop('now')"));
// display final tree as HTML…
$xml->formatOutput = true;
echo "<xmp>" . $xml->saveXML() . "</xmp>";
// …or write it to a file as XML
$xml->save("list.xml") or die("ERROR: Could not write to file");
?>
Of course, an XML document is much more than just elements—which is why the DOM extension also offers createTextNode(), createCDATASection(), createProcessingInstruction(), and createComment() methods to attach text, CDATA blocks, PIs (Processing Instructions), and comments to the DOM tree respectively. Attributes for an element are set by calling the corresponding element object's setAttribute() method with appropriate parameters.
Once the tree has been generated, it may be retrieved as a string with the primary DOMDocument object's saveXML() method, or written to a file with the object's save() method.
<?php
// initialize DOM object
$xml = new DOMDocument("1.0");
// add root node <listing>
$root = $xml->createElement("list");
$xml->appendChild($root);
// add element <description> to root
$desc = $xml->createElement("description");
$root->appendChild($desc);
// add <description> content
$desc->appendChild($xml->createTextNode("Sam's Shopping List"));
// add comment
$root->appendChild($xml->createComment("item list follows"));
// add <item> child element
// add quantities as attributes
$items = $xml->createElement("items");
$root->appendChild($items);
$item = $xml->createElement("item");
$items->appendChild($item);
$item->appendChild($xml->createTextNode("eggs"));
$item->setAttribute("units", 3);
unset($item);
$item = $xml->createElement("item");
$items->appendChild($item);
$item->appendChild($xml->createTextNode("salt"));
$item->setAttribute("units", "100 gm");
// add CDATA block
$items->appendChild($xml->createCDATASection("You can't make↩
an omelette without breaking eggs"));
// add PI
$root->appendChild($xml->createProcessingInstruction(↩
"xml-dummy-pi", "shop('now')"));
// display final tree as HTML…
$xml->formatOutput = true;
echo "<xmp>" . $xml->saveXML() . "</xmp>";
// …or write it to a file as XML
$xml->save("list.xml") or die("ERROR: Could not write to file");
?>
Comments
PHP's SimpleXML extension does not support node creation, so this task is better handled with PHP's DOM extension, which comes with a wide array of methods designed to help you design an XML document instance dynamically. To get started, create an instance of the DOMDocument class, and then use its createElement() method to create element objects. These element objects may then be attached to a parent node by calling the parent node object's appendChild() method. The process is illustrated in the previous listing.Of course, an XML document is much more than just elements—which is why the DOM extension also offers createTextNode(), createCDATASection(), createProcessingInstruction(), and createComment() methods to attach text, CDATA blocks, PIs (Processing Instructions), and comments to the DOM tree respectively. Attributes for an element are set by calling the corresponding element object's setAttribute() method with appropriate parameters.
Once the tree has been generated, it may be retrieved as a string with the primary DOMDocument object's saveXML() method, or written to a file with the object's save() method.
相关文章推荐
- PHP通过DOM操作XML
- PHP生成XML的类
- PHP往XML中添加节点的方法
- php学习——xml
- weiphp 安装( 解决 -1:fail to get xml content 问题)
- php 解析xml 的四种方法(转)
- php xml
- php开发安卓服务器之 php封装json xml通用app数据通信接口
- php生成xml简单实例代码
- PHP输出XML到页面的3种方式
- [转]php使用DOM处理xml文档
- php xmlreader simplexml等读取xml
- Php 创建XML
- PHP XML To Array,将XML转换为数组
- php十行代码将xml转成数组
- php 数组转xml 数组转json xml转数组 json转数组
- PHP_XML_Expat
- php 删除 XML 中的一个元素
- 韩顺平 PHP视频教程 Ajax技术第05讲-返回XML如何处理
- php中XMLHttpRequest(Ajax)不能设置自定义的Referer的解决方法