XML on Server

XML files are plain text files similar to HTML files.

Can be easily stored and generated on standard web servers.

Storing XML Files on the Server

The way XML files are stored on Internet servers is exactly the same as HTML files.

Please open Windows Notepad and enter the following code:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <from>John</from>
  <to>George</to>
  <message>Remember me this weekend</message>
</note>

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

Generating XML with PHP

You can generate XML on the server without installing any XML software.

To generate XML responses on the server using PHP, please use the following code:

<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<note>";
echo "<from>John</from>";
echo "<to>George</to>";
echo "<message>Remember me this weekend</message>";
echo "</note>";
?>

Please note that the content type of the response header must be set to "text/xml".

See how this PHP file is returned from the server.

If you want to learn PHP, please read our PHP tutorial.

Generate XML through ASP

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

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

<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='UTF-8'?>")
response.Write("<note>")
response.Write("<from>John</from>")
response.Write("<to>George</to>")
response.Write("<message>Remember me this weekend</message>")
response.Write("</note>")
%>

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

See how this ASP file is returned from the server

If you want to learn ASP, you can read our ASP tutorials.

Generate XML from a database

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

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

<%
response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("/datafolder/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>")
%>

View the actual database output of the above ASP code

The above example uses ASP with ADO.

If you need to learn ADO, please visit our 'ADO Tutorial'.

Use XSLT to convert XML on the server

The following ASP code converts an XML file to HTML 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

  1. The first code block creates an instance of the Microsoft XML parser (XMLDOM) and loads the XML file into memory
  2. The second code block creates another instance of the parser and loads the XSL file into memory
  3. The last line of code uses an XSL document to transform the XML document and send the result as HTML to the browser. Job done!

See how the above code runs

Save XML as a file using ASP

This ASP instance will create a simple XML document and save the document to the server:

<%
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")
%>