選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

XML 教程

XML 首頁 XML 簡介 XML 如何使用 XML 樹 XML 語法 XML 元素 XML 屬性 XML 名稱空間 XML 顯示 XML HttpRequest XML 解析器 XML DOM XML XPath XML XSLT XML XQuery XML XLink XML 驗證器 XML DTD XML Schema XML 伺服器 XML 示例 XML 測驗 XML 證書

XML AJAX

AJAX Introduction AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

XML DOM

DOM Introduction DOM Nodes DOM Accessing DOM Node Info DOM Node List DOM Traversing DOM Navigating DOM Get Values DOM Change Nodes DOM Remove Nodes DOM Replace Nodes DOM Create Nodes DOM Add Nodes DOM Clone Nodes DOM Examples

XPath 教程

XPath Introduction XPath Nodes XPath Syntax XPath Axes XPath Operators XPath Examples

XSLT 教程

XSLT Introduction XSL Languages XSLT Transform XSLT <template> XSLT <value-of> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <choose> XSLT Apply XSLT on the Client XSLT on the Server XSLT Edit XML XSLT Examples

XQuery 教程

XQuery Introduction XQuery Example XQuery FLWOR XQuery HTML XQuery Terms XQuery Syntax XQuery Add XQuery Select XQuery Functions

XML DTD

DTD Introduction DTD Building Blocks DTD Elements DTD Attributes DTD Elements vs Attr DTD Entities DTD Examples

XSD Schema

XSD 簡介 XSD 操作方法 XSD <schema> XSD 元素 XSD 屬性 XSD 限制 XSD 複雜元素 XSD 空 XSD 僅元素 XSD 僅文字 XSD 混合 XSD 指示符 XSD <any> XSD <anyAttribute> XSD 替換 XSD 示例

XSD Data Types

XSD String XSD Date/Time XSD Numeric XSD Misc XSD Reference

Web Services

XML Services XML WSDL XML SOAP XML RDF XML RSS

參考手冊

DOM 節點型別 DOM 節點 DOM 節點列表 DOM NamedNodeMap DOM 文件 DOM 元素 DOM 屬性 DOM 文字 DOM CDATA DOM 註釋 DOM XMLHttpRequest DOM 解析器 XSLT 元素 XSLT/XPath 函式

伺服器上的 XML


XML 檔案與 HTML 檔案一樣,都是純文字檔案。

XML 可以很容易地由標準 Web 伺服器儲存和生成。


在伺服器上儲存 XML 檔案

XML 檔案可以像 HTML 檔案一樣儲存在 Internet 伺服器上。

啟動 Windows 記事本並寫入以下行

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <from>Jani</from>
  <to>Tove</to>
  <message>本週末記得我</message>
</note>

將檔案儲存到您的 Web 伺服器,並命名為“note.xml”之類的適當名稱。


使用 PHP 生成 XML

可以在伺服器上生成 XML,而無需安裝任何 XML 軟體。

要使用 PHP 從伺服器生成 XML 響應,請使用以下程式碼

<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<note>";
echo "<from>Jani</from>";
echo "<to>Tove</to>";
echo "<message>本週末記得我</message>";
echo "</note>";
?>

請注意,響應頭的 Content-type 必須設定為 "text/xml"。

檢視 PHP 檔案將如何從伺服器返回.

如果您想學習 PHP,請在我們的主頁上找到我們的 PHP 教程。



使用 ASP 生成 XML

要從伺服器生成 XML 響應 - 只需編寫以下程式碼並將其作為 ASP 檔案儲存在 Web 伺服器上

<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='UTF-8'?>")
response.Write("<note>")
response.Write("<from>Jani</from>")
response.Write("<to>Tove</to>")
response.Write("<message>本週末記得我</message>")
response.Write("</note>")
%>

請注意,響應的 Content-type 必須設定為 "text/xml"。

檢視 ASP 檔案將如何從伺服器返回.

如果您想學習 ASP,請在我們的主頁上找到我們的 ASP 教程。


從資料庫生成 XML

可以在不安裝任何 XML 軟體的情況下從資料庫生成 XML。

要從伺服器生成 XML 資料庫響應,只需編寫以下程式碼並將其作為 ASP 檔案儲存在 Web 伺服器上

<%
response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("/資料資料夾/database.mdb")

sql="select fname,lname from tblGuestBook"
set rs=Conn.Execute(sql)

response.write("<?xml version='1.0' encoding='UTF-8'?>")
response.write("<guestbook>")
while (not rs.EOF)
response.write("<guest>")
response.write("<fname>" & rs("fname") & "</fname>")
response.write("<lname>" & rs("lname") & "</lname>")
response.write("</guest>")
rs.MoveNext()
wend

rs.close()
conn.close()
response.write("</guestbook>")
%>

檢視上述 ASP 檔案中的真實資料庫輸出.

上面的示例使用 ASP 和 ADO。

如果您想學習 ASP 和 ADO,請在我們的主頁上找到相關教程。


在伺服器上使用 XSLT 轉換 XML

此 ASP 在伺服器上將 XML 檔案轉換為 XHTML

<%
'載入 XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("simple.xml"))

'載入 XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("simple.xsl"))

'轉換檔案
Response.Write(xml.transformNode(xsl))
%>

示例說明

  • 第一段程式碼建立了一個 Microsoft XML 解析器(XMLDOM)例項,並將 XML 檔案載入到記憶體中。
  • 第二段程式碼建立了另一個解析器例項,並將 XSL 檔案載入到記憶體中。
  • 最後一行程式碼使用 XSL 文件轉換 XML 文件,並將結果作為 XHTML 傳送到您的瀏覽器。太棒了!

檢視它是如何工作的.


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援