XML on the server


May 27, 2021 19:00 XML


Table of contents


XML on the server

If you want your browser to have access to web projects, you need to configure the Web .XML server. Once the configuration is unsuccessful, the browser cannot access it.

An XML file is a plain text file that resembles an HTML file.

XML can be easily stored and generated through standard Web servers.


Store XML files on the server

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

Start Windows Note book 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 save the file on a Web server .xml file name, such as "note."


XML is generated from the ASP

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

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

<%
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 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 the ASP tutorial on our home page.


XML is generated from PHP

To generate an XML response from the server using PHP, 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>";
?> 

Note that the content type of 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 the PHP tutorial on our home page.


XML is generated from the 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("<?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>")
%> 

View the actual database output of the ASP file above.

The above example uses an ASP with an ADO.

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


XML is converted through XSLT on the server

The following ASP code converts the XML file to 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))
%> 

The instance explanation

  • The first block of code creates an instance of the Microsoft XML Parser (XMLDOM) and loads the XML file into memory.
  • The second block of code creates another instance of the parser and loads the XSL file into memory.
  • The last code uses the XSL document to transform the XML document and send the results to your browser in XHTML.

See how the code above runs.


Save XML as a file through your ASP

This ASP instance creates a simple XML document and saves it 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")
%> 

That's the whole story of this section, and in the next section we'll introduce you to the advanced XML DOM.