溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP如何輸出XML格式數(shù)據(jù)

發(fā)布時間:2021-07-30 14:03:13 來源:億速云 閱讀:224 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)PHP如何輸出XML格式數(shù)據(jù),小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

具體如下:

方法1:

<?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>";
?>

上述示例運(yùn)行效果圖如下:

PHP如何輸出XML格式數(shù)據(jù)

方法2:

<?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>";
?>

方法3:

<?php
header('Content-Type: text/xml;');
//創(chuàng)建新的xml文件
$dom = new DOMDocument('1.0', 'utf-8');
//建立<response>元素
$response = $dom->createElement('response');
$dom->a($response);
//建立<books>元素并將其作為<response>的子元素
$books = $dom->createElement('books');
$response->a($books);
//為book創(chuàng)建標(biāo)題
$title = $dom->createElement('title');
$titleText = $dom->createTextNode('PHP與AJAX');
$title->a($titleText);
//為book創(chuàng)建isbn元素
$isbn = $dom->createElement('isbn');
$isbnText = $dom->createTextNode('1-21258986');
$isbn->a($isbnText);
//創(chuàng)建book元素
$book = $dom->createElement('book');
$book->a($title);
$book->a($isbn);
//將<book>作為<books>子元素
$books->a($book);
//在一字符串變量中建立XML結(jié)構(gòu)
$xmlString = $dom->saveXML();
//輸出XML字符串
echo $xmlString;
?>

1.php輸出xml的實(shí)例

header('Content-Type: text/xml');
$sql = "select * from article order by post_date limit 100";
$re = $db->query($sql);
echo "<?xml version="1.0" encoding="utf-8"?>\n";
echo "<data>\n";
while($row = $db->fetchArray($re)){
echo "<event ";
echo "start="".strftime ("%b %d %Y %H:%M:%S", $row['post_date'])."" ";
echo "title="".htmlspecialchars($row['title'])."" ";
echo "link="tdetail.php?id=".$row['time_point'].""";
echo ">";
echo htmlspecialchars($row['content']);
echo "</event>\n";
}
echo "</data>";

2.用JavaScript操作XML 實(shí)例

文中盡量用上所有的操作和屬性,以做示范

<script language="JavaScript">
<!--
var doc = new ActiveXObject("Msxml2.DOMDocument"); //ie5.5+,CreateObject("Microsoft.XMLDOM")
//加載文檔
//doc.load("b.xml");
//創(chuàng)建文件頭
var p = doc.createProcessingInstruction("xml","version='1.0'  encoding='gb2312'");
//添加文件頭
doc.a(p);
//用于直接加載時獲得根接點(diǎn)
//var root = doc.documentElement;
//兩種方式創(chuàng)建根接點(diǎn)
// var root = doc.createElement("students");
var root = doc.createNode(1,"students","");
//創(chuàng)建子接點(diǎn)
var n = doc.createNode(1,"ttyp","");
//指定子接點(diǎn)文本
//n.text = " this is a test";
//創(chuàng)建孫接點(diǎn)
var o = doc.createElement("sex");
o.text = "男"; //指定其文本
//創(chuàng)建屬性
var r = doc.createAttribute("id");
r.value="test";
//添加屬性
n.setAttributeNode(r);
//創(chuàng)建第二個屬性
var r1 = doc.createAttribute("class");
r1.value="tt";
//添加屬性
n.setAttributeNode(r1);
//刪除第二個屬性
n.removeAttribute("class");
//添加孫接點(diǎn)
n.a(o);
//添加文本接點(diǎn)
n.a(doc.createTextNode("this is a text node."));
//添加注釋
n.a(doc.createComment("this is a comment\n"));
//添加子接點(diǎn)
root.a(n);
//復(fù)制接點(diǎn)
var m = n.cloneNode(true);
root.a(m);
//刪除接點(diǎn)
root.removeChild(root.childNodes(0));
//創(chuàng)建數(shù)據(jù)段
var c = doc.createCDATASection("this is a cdata");
c.text = "hi,cdata";
//添加數(shù)據(jù)段
root.a(c);
//添加根接點(diǎn)
doc.a(root);
//查找接點(diǎn)
var a = doc.getElementsByTagName_r("ttyp");
//var a = doc.selectNodes("//ttyp");
//顯示改接點(diǎn)的屬性
for(var i= 0;i<a.length;i++)
{
    alert(a.xml);
    for(var j=0;j<a.attributes.length;j++)
    {
     alert(a.attributes[j].name);
    }
}
//修改節(jié)點(diǎn),利用XPATH定位節(jié)點(diǎn)
var b = doc.selectSingleNode("//ttyp/sex");
b.text = "女";
//alert(doc.xml);
//XML保存(需要在服務(wù)端,客戶端用FSO)
//doc.save();
//查看根接點(diǎn)XML
if(n)
{
    alert(n.ownerDocument.xml);
}
//-->
</script>

關(guān)于“PHP如何輸出XML格式數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI