|
XML can be generated on a server without any installed XML
controls.
Storing XML on the Server
XML files can be stored on your Internet server.
XML files can be stored on your Internet server, just like any
other HTML files.
Start up your Notepad editor and write the following lines:
<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Remember me this weekend</message>
</note>
All you have to do is to save the file on your Internet server
with a proper name like "note.xml", before the XML document is
ready to use.
Note: The XML file must be in the same
directory (folder) as your HTML files, and the MIME type of XML
files should be set to text/xml.
Generating XML with ASP
XML can be generated on a server without any installed XML
software.
To generate an XML response from your server - simply write the
following code and save it as an ASP file on your web server :
<%Response.ContentType="text/xml"
Response.Write("<?xml version='1.0' ?>")
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 XML.
(ASP stands for Active Server Pages. If you don't know how to
write ASP, you can study it at
ASP
Getting XML from a Database
XML can be generated from a database without any installed XML
software.
The XML response from the previous example can easily be
modified to fetch its data from a database.
To generate an XML database response from the server, simply
write the following code and save it as an ASP file:
<% Response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection") conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("../ado/database.mdb") sql="select
fname, lname from tblGuestBook" set rs = Conn.Execute(sql)
rs.MoveFirst() response.write("<?xml version='1.0' ?>")
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>") %>
The example above uses ASP with ADO. If you don't know how to
use ADO, you can study it at
ADO
This chapter demonstrates a small framework for an XML
application.
Start with an XML document
First we start with a simple XML document.
Take a look at our original demonstration document, the CD
catalog.
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD> . . ... more ... .
Load the document into a Data Island
A Data Island can be used to access the XML file.
To get your XML document "inside" an HTML page, add an XML Data
Island to the page.
<xml src="cd_catalog.xml" id="xmldso" async="false">
</xml>
With the example code above, the XML file "cd_catalog.xml" will
be loaded into an "invisible" Data Island called "xmldso". The
async="false" attribute is added to the Data Island to make sure
that all the XML data is loaded before any other HTML processing
takes place.
Bind the Data Island to an HTML Table
An HTML table can be used to display the XML data.
To make your XML data visible on your HTML page, you must "bind"
your XML Data Island to an HTML element.
To bind your XML data to an HTML table, add a data source
attribute to the table, and add data field attributes to <span>
elements inside the table data:
<table datasrc="#xmldso" width="100%" border="1">
<thead>
<th>Title</th>
<th>Artist</th>
<th>Year</th>
</thead>
<tr align="left">
<td><span datafld="TITLE"></span></td>
<td><span datafld="ARTIST"></span></td>
<td><span datafld="YEAR"></span></td>
</tr>
</table>
Bind the Data Island to <span> or <div> elements
<span> or <div> elements can be used to display XML data.
You don't have to use a table to display your XML data. Data
from a Data Island can be displayed anywhere on an HTML page.
All you have to do is to add some <span> or <div> elements to
your page. Use the data source attribute to bind the elements to
the Data Island, and the data field attribute to bind each
element to an XML element, like this:
<br>
Title: <span datasrc="#xmldso" datafld="TITLE"></span>
<br>
Artist: <span datasrc="#xmldso" datafld="ARTIST"></span>
<br>
Year: <span datasrc="#xmldso" datafld="YEAR"></span>
or like this:
<br>
Title: <div datasrc="#xmldso" datafld="TITLE"></div>
<br>
Artist: <div datasrc="#xmldso" datafld="ARTIST"></div>
<br>
Year: <div datasrc="#xmldso" datafld="YEAR"></div>
Note that if you use a <div> element, the data will be displayed
on a new line.
With the examples above, you will only see one line of your XML
data. To navigate to the next line of data, you have to add some
scripting to your code.
Add a Navigation Script to your XML
Navigation has to be performed by a script.
To add navigation to the XML Data Island, create a script that
calls the movenext() and moveprevious() methods of the Data
Island.
<script language="JavaScript">
function movenext()
{
x=xmldso.recordset
if (x.absoluteposition < x.recordcount)
{
x.movenext()
}
}
function moveprevious()
{
x=xmldso.recordset
if (x.absoluteposition > 1)
{
x.moveprevious()
}
}
</script>
All Together Now
With a little creativity you can create a full application.
If you use what you have learned on this page, and a little
imagination, you can easily develop this into a full
application.
|