Latest web development tutorials

XML on the server

XML files are plain text files similar to HTML files.

XML through a standard Web server can easily store and generation.


XML files are stored on the server

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

Start Windows Notepad and write the following lines:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Remember me this weekend</message>
</note>

Then use the appropriate file name, such as "note.xml", save this file on the Web server.


XML generated by ASP

XML can without installing any software to generate XML on the server side.

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

<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<note>")
response.Write("<from>Jani</from>")
response.Write("<to>Tove</to>")
response.Write("<message>Remember me this weekend</message>")
response.Write("</note>")
%>

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

See how the ASP file is returned from the server .

If you want to learn ASP, please on our home page to find ASP tutorial.


Generate XML with PHP

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

<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
echo "<note>";
echo "<from>Jani</from>";
echo "<to>Tove</to>";
echo "<message>Remember me this weekend</message>";
echo "</note>";
?>

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

Show how PHP files returned from the server .

If you want to learn PHP, please our home page to find PHP tutorials.


XML generated from a database

XML can be generated from the database without any installed 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("<?xml version='1.0' encoding='ISO-8859-1'?>")
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>")
%>

See actual database file output than ASP .

The example above uses ASP with ADO in.

If you want to learn ASP and ADO, please on our home page to find tutorials.


On the server via XSLT transform XML

The following ASP code on the server to XML files into XHTML:

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

Examples explained

  • The first block of code creates an instance of Microsoft's XML parser (XMLDOM), and the XML file into memory.
  • The second block of code creates another instance of the parser, and the XSL file into memory.
  • Finally, the code uses a XSL document to transform XML documents, XHTML and the results sent to your browser.

Take a look at how to run the above code .


By ASP saved XML file

The ASP instances create a simple XML document and saves the document to the server:

<%
text="<note>"
text=text & "<to>Tove</to>"
text=text & "<from>Jani</from>"
text=text & "<heading>Reminder</heading>"
text=text & "<body>Don't forget me this weekend!</body>"
text=text & "</note>"

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async=false
xmlDoc.loadXML(text)

xmlDoc.Save("test.xml")
%>