您的位置:首页 > 编程语言 > PHP开发

php生成xml简单实例代码

2011-11-18 17:38 776 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: