XML on the Server

Similar to HTML files, XML files are plain text files that can easily be stored and generated by a standard web server.

Storing XML Files on the Server:

In the same way as HTML files, XML files can be stored on an Internet server. Write the following lines after starting Windows Notepad.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <from>Tom</from>
  <to>Sapna</to>
  <message>Meeting on Monday Morning at 11 AM.</message>
</note>

Explanation:

The file should be saved on the webserver with a proper name.

Generating XML with PHP:

Even without any installed XML software, XML can be generated on a server.

Example: To generate an XML response from the server using PHP

<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<note>";
echo "<from>Tom</from>";
echo "<to>Sapna</to>";
echo "<message>Meeting on Monday Morning at 11 AM.</message>";
echo "</note>";
?>

Explanation:

The “text/xml” is what the content type of the response header should be set to.

Generating XML with ASP:

We will write the below code and will save it as an ASP file on the webserver to generate an XML response from the server.

Example:

<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='UTF-8'?>")
response.Write("<note>")
response.Write("<from>Tom</from>")
response.Write("<to>Sapna</to>")
response.Write("<message>Meeting on Monday Morning at 11 AM.</message>")
response.Write("</note>")
%>

Explanation:

The “text/xml” is what the content type of the response should be set to.

Generating XML From a Database:

It is not necessary to install XML software to generate XML from a database. We will write the below code and will save it as an ASP file on the webserver to generate an XML database response from the server.

Example:

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

Explanation:

In the above example, we are using ASP with ADO.

Transforming XML with XSLT on the Server:

Example:

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("new.xml"))
 
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("new.xsl"))
 
'Transform file
Response.Write(xml.transformNode(xsl))
%>

Explanation:

In the above example, the ASP is transforming the XML file to XHTML on the server. An instance of the Microsoft XML parser (XMLDOM) is created and the XML file is loaded into memory in the first block of the code. Another instance of the parser is created and the XSL file is loaded into the memory in the second block of the code. The XML document is transformed using the XSL document in the last line of the code. The result is sent as XHTML to our browser.

Please Share