服務(wù)器上的 XML
XML 文件是類似 HTML 文件的純文本文件。
能夠通過標(biāo)準(zhǔn)的 web 服務(wù)器輕松地存儲(chǔ)和生成 XML。
在服務(wù)器上存儲(chǔ) XML
XML 文件在 Internet 服務(wù)器上進(jìn)行存儲(chǔ)的方式與 HTML 文件完全相同。
請(qǐng)打開 Windows 記事本,并輸入以下代碼:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <from>John</from> <to>George</to> <message>Don't forget the meeting!</message> </note>
然后用適當(dāng)?shù)奈募,比?"note.xml",在 web 服務(wù)器上保存這個(gè)文件。
通過 ASP 生成 XML
XML 可在不安裝任何 XML 軟件的情況下在服務(wù)器端生成。
如需從服務(wù)器生成 XML 響應(yīng) - 只需簡(jiǎn)單地編寫以下代碼并在服務(wù)器上把它保存為一個(gè) ASP 文件:
<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<note>")
response.Write("<from>John</from>")
response.Write("<to>George</to>")
response.Write("<message>Don't forget the meeting!</message>")
response.Write("</note>")
%>
請(qǐng)注意,此響應(yīng)的內(nèi)容類型必須設(shè)置為 "text/xml"。
如果您還不懂如何編寫 ASP,請(qǐng)?jiān)L問我們的《ASP 教程》。
通過 PHP 生成 XML
如需使用 PHP 在服務(wù)器上生成 XML 響應(yīng),請(qǐng)使用下面的代碼:
<?php
header("Content-type:text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
echo "<note>";
echo "<from>John</from>";
echo "<to>George</to>";
echo "<message>Don't forget the meeting!</message>";
echo "</note>";
?>
請(qǐng)注意,響應(yīng)頭部的內(nèi)容類型必須設(shè)置為 "text/xml"。
如果您需要學(xué)習(xí) PHP,請(qǐng)?jiān)L問我們的《PHP 教程》。
從數(shù)據(jù)庫獲取 XML
XML 可在不安裝任何 XML 軟件的情況下從數(shù)據(jù)庫生成。
如需從服務(wù)器生成 XML 數(shù)據(jù)庫響應(yīng),只需簡(jiǎn)單地編寫以下代碼,并把它在服務(wù)器上保存為 ASP 文件:
<% response.ContentType = "text/xml" set conn=Server.CreateObject("ADODB.Connection") conn.provider="Microsoft.Jet.OLEDB.4.0;" conn.open server.mappath("/db/database.mdb") sql="select FirstName,LastName from Persons" set rs=Conn.Execute(sql) rs.MoveFirst() response.write("<?xml version='1.0' encoding='ISO-8859-1'?>") response.write("<Customers>") while (not rs.EOF) response.write("<Person>") response.write("<FirstName>" & rs("FirstName") & "</FirstName>") response.write("<LastName>" & rs("LastName") & "</LastName>") response.write("</Person>") rs.MoveNext() wend rs.close() conn.close() response.write("</Customers>") %>
查看以上 ASP 代碼的實(shí)際數(shù)據(jù)庫輸出案例
上面的例子使用了帶有 ADO 的 ASP。
如果您需要學(xué)習(xí) ADO,請(qǐng)?jiān)L問我們的《ADO 教程》。
在服務(wù)器上通過 XSLT 轉(zhuǎn)換 XML
下面的 ASP 代碼在服務(wù)器上把 XML 文件轉(zhuǎn)換為 XHTML:
<% 'Load XML set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("simple.xml")) 'Load XSL set xsl = Server.CreateObject("Microsoft.XMLDOM") xsl.async = false xsl.load(Server.MapPath("simple.xsl")) 'Transform file Response.Write(xml.transformNode(xsl)) %>
例子解釋
- 第一個(gè)代碼塊創(chuàng)建微軟 XML 解析器的實(shí)例 (XMLDOM),并把 XML 文件載入內(nèi)存
- 第二個(gè)代碼塊創(chuàng)建解析器的另一個(gè)實(shí)例,并把 XSL 文件載入內(nèi)存
- 最后一個(gè)代碼使用 XSL 文檔來轉(zhuǎn)換 XML 文檔,并把結(jié)果以 XHTML 發(fā)送到您的瀏覽器。完工。
通過 ASP 把 XML 保存為文件
這個(gè) ASP 實(shí)例會(huì)創(chuàng)建一個(gè)簡(jiǎn)單的 XML 文檔,并把該文檔保存到服務(wù)器上:
<% text="<note>" text=text & "<to>George</to>" text=text & "<from>John</from>" text=text & "<heading>Reminder</heading>" text=text & "<body>Don't forget the meeting!</body>" text=text & "</note>" set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(text) xmlDoc.Save("test.xml") %>