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:
Tom Sapna Meeting on Monday Morning at 11 AM.
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
"; echo ""; echo " "; ?>Tom "; echo "Sapna "; echo "Meeting on Monday Morning at 11 AM. "; echo "
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("") response.Write("") response.Write(" ")Tom ") response.Write("Sapna ") response.Write("Meeting on Monday Morning at 11 AM. ") response.Write("
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("") response.write("") while (not rs.EOF) response.write(" ")") response.write(" ") rs.MoveNext() wend rs.close() conn.close() response.write("" & rs("fname") & " ") response.write("" & rs("lname") & " ") response.write("
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.