Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML on the Server

XML on the Server


XML files are plain text files, similar to HTML files.

XML can be easily stored and generated by standard web servers.


Storing XML Files on the Server

XML files are stored on an Internet server in exactly the same way as HTML files.

Start Windows Notepad and write the following lines:

Jani

Tove

Remember me this weekend

Then save the file on the web server with an appropriate name, such as "note.xml".


Generating XML with ASP

XML can be generated on the server side without installing any XML software.

To generate an XML response from the server - simply write the following code and save it as an ASP file on the web server:

<%

response.ContentType="text/xml"

response.Write("")

response.Write("")

response.Write("Jani")

response.Write("Tove")

response.Write("Remember me this weekend")

response.Write("")

%>

Note that the content type of this response must be set to "text/xml".

See how this ASP file returns from the server。

If you want to learn ASP, please find theHomeASP tutorial on our site.


Generating XML with PHP

To generate an XML response from the server using PHP, use the following code:

";

echo "";

echo "Jani";

echo "Tove";

echo "Remember me this weekend";

echo "";

?>

Note that the content type in the response header must be set to "text/xml".

See how this PHP file returns from the server。

If you want to learn PHP, please find theHomePHP tutorial on our site.


Generating XML from a Database

XML can be generated from a database without installing any XML software.

To generate an XML database response from the server, simply write the following code and save it as an ASP file on the web server:

<%

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 fname,lname from tblGuestBook"

set rs=Conn.Execute(sql)

response.write("")

response.write("")

while (not rs.EOF)

response.write("")

response.write("" & rs("fname") & "")

response.write("" & rs("lname") & "")

response.write("")

rs.MoveNext()

wend

rs.close()

conn.close()

response.write("")

%>

View the actual database output of the ASP file above。

The example above uses ASP with ADO.

If you want to learn ASP and ADO, please find theHomerelevant tutorial on our site.


Transforming XML with XSLT on the Server

The following ASP code transforms an XML file into XHTML on the server:

<%

'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))

%>

Example Explanation

See how the code above works。


Saving XML as a File with ASP

This ASP example creates a simple XML document and saves it on the server:

<%

text=""

text=text & "Tove"

text=text & "Jani"

text=text & "Reminder"

text=text & "Don't forget me this weekend!"

text=text & ""

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")

xmlDoc.async=false

xmlDoc.loadXML(text)

xmlDoc.Save("test.xml")

%>