PHP输出XML到页面的3种方法详解
第一种方法:
<?php
header("content-type: text/xml");
echo "<?xml version=/"1.0/" encoding=/"utf-8/"?>";
echo "<users>";
echo "<user>";
echo "<name>";
echo "小小菜鸟";
echo "</name>";
echo "<age>";
echo "24";
echo "</age>";
echo "<sex>";
echo "男";
echo "</sex>";
echo "</user>";
echo "<user>";
echo "<name>";
echo "艳艳";
echo "</name>";
echo "<age>";
echo "23";
echo "</age>";
echo "<sex>";
echo "女";
echo "</sex>";
echo "</user>";
echo "</users>";
?>
第二种方法:
<?php
header("content-type: text/xml");
echo "<?xml version=/"1.0/" encoding=/"utf-8/"?>";
echo "<users><user><name>小小菜鸟</name><age>24</age><sex>男</sex></user><user><name>艳艳</name><age>23</age><sex>女</sex></user></users>";
?>
第三种方法:
<?php
/*
用php的dom控件来创建xml输出
设置输出内容的类型为xml
*/
header('content-type: text/xml;');
//创建新的xml文件
$dom = new domdocument('1.0', 'utf-8');
//建立<response>元素
$response = $dom->createelement('response');
$dom->appendchild($response);
//建立<books>元素并将其作为<response>的子元素
$books = $dom->createelement('books');
$response->appendchild($books);
//为book创建标题
$title = $dom->createelement('title');
$titletext = $dom->createtextnode('php与ajax');
$title->appendchild($titletext);
//为book创建isbn元素
$isbn = $dom->createelement('isbn');
$isbntext = $dom->createtextnode('1-21258986');
$isbn->appendchild($isbntext);
//创建book元素
$book = $dom->createelement('book');
$book->appendchild($title);
$book->appendchild($isbn);
//将<book>作为<books>子元素
$books->appendchild($book);
//在一字符串变量中建立xml结构
$xmlstring = $dom->savexml();
//输出xml字符串
echo $xmlstring;
?>
上一篇: 字体不能随便用,侵权竟被索赔上百万!
下一篇: 搜狗输入法与VS快捷键有冲突_处理办法