PHP输出XML到页面的3种方法详解_PHP教程
程序员文章站
2022-04-25 11:05:05
...
第一种方法:
header("Content-type: text/xml");
echo "";
echo "";
echo "";
echo "";
echo "小小菜鸟";
echo " ";
echo "";
echo "24";
echo " ";
echo "";
echo "男";
echo " ";
echo " ";
echo "";
echo "";
echo "艳艳";
echo " ";
echo "";
echo "23";
echo " ";
echo "";
echo "女";
echo " ";
echo " ";
echo " ";
?>
第二种方法:
header("Content-type: text/xml");
echo "";
echo "小小菜鸟 24 男 艳艳 23 女 ";
?>
第三种方法:
/*
用PHP的DOM控件来创建XML输出
设置输出内容的类型为xml
*/
header('Content-Type: text/xml;');
//创建新的xml文件
$dom = new DOMDocument('1.0', 'utf-8');
复制代码 代码如下:
header("Content-type: text/xml");
echo "";
echo "
echo "
echo "
echo "小小菜鸟";
echo "
echo "
echo "24";
echo "
echo "
echo "男";
echo "
echo "
echo "
echo "
echo "艳艳";
echo "
echo "
echo "23";
echo "
echo "
echo "女";
echo "
echo "
echo "
?>
第二种方法:
复制代码 代码如下:
header("Content-type: text/xml");
echo "";
echo "
?>
第三种方法:
复制代码 代码如下:
/*
用PHP的DOM控件来创建XML输出
设置输出内容的类型为xml
*/
header('Content-Type: text/xml;');
//创建新的xml文件
$dom = new DOMDocument('1.0', 'utf-8');
//建立
$response = $dom->createElement('response');
$dom->appendChild($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);
//将
$books->appendChild($book);
//在一字符串变量中建立XML结构
$xmlString = $dom->saveXML();
//输出XML字符串
echo $xmlString;
?>