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:
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("
response.Write("
response.Write("
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 "
echo "
echo "
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("
response.write("
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
- The first code block creates an instance of the Microsoft XML parser (XMLDOM) and loads the XML file into memory.
- The second code block creates another instance of the parser and loads the XSL file into memory.
- The last block of code uses the XSL document to transform the XML document and sends the result to your browser as XHTML.
Saving XML as a File with ASP
This ASP example creates a simple XML document and saves it on the server:
<%
text="
text=text & "
text=text & "
text=text & "
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")
%>