php生成xml简单实例代码

php生成xml简单实例代码 <?php // create doctype $dom = new DOMDocument("1.0"); // display document in browser as plain text // for readability purposes header("Content-Type: text/plain"); // create root element $root = $dom->createElement("channel"); $dom->appendChild($root); // create child element $item = $dom->createElement("item"); $root->appendChild($item); //rss $title = $dom->createElement("title"); $item->appendChild($title); //$text = $dom->createTextNode("标题测试1"); //$title = appendChild($text); //Fatal error: Call to undefined function appendChild() $link = $dom->createElement("link"); $item->appendChild($link); //$text = $dom->createTextNode("链接地址测试1"); //$link = appendChild($text); //Fatal error: Call to undefined function appendChild() //create child element $item = $dom->createElement("item"); $root->appendChild($item); //create another text node $text = $dom->createTextNode("tomato"); $item->appendChild($text); //create attribute node $title = $dom->createAttribute("title"); $item->appendChild($title); //create attribute value node $titleValue = $dom->createTextNode("red"); $title->appendChild($titleValue); //create CDATA section $cdata = $dom->createCDATASection("Customer requests that pizza be sliced into 16 square picess"); $root->appendChild($cdata); //create PI $pi = $dom->createProcessingInstruction("pizza","bake()"); $root->appendChild($pi); // save and display tree echo $dom->saveXML(); ?>   结果如下: <?xml version="1.0"?> <channel><item><title/><link/></item><item title="red">tomato</item><![CDATA[Customer requests that pizza be sliced into 16 square picess]]><?pizza bake()?></channel> http://www.xieguang133.com  

posted on 2011-11-18 17:38  xieguang133  阅读(115)  评论(0)    收藏  举报

导航