XML in ASP
In ASP you can easily parse the XML-DOM (Document Object Model) using the MSXML component.
Download the complete sample project for this.
The following functions will load an XML file from the file-system:
function getXml(xmlPath) dim xmlDoc dim isValid set xmlDoc = server.createObject("Microsoft.XMLDOM") xmlDoc.async = false xmlDoc.load server.mapPath(xmlPath) isValid = cBool(xmlDoc.parseError.errorCode = 0) if not isValid then response.write getXMLError(xmlDoc) end if set getXml = xmlDoc end function function getXMLError(xmlDoc) dim strError strError = "Invalid XML file!" & vbNewline & _ "File: " & xmlDoc.parseError.url & vbNewline & _ "Line: " & xmlDoc.parseError.line & vbNewline & _ "Character: " & xmlDoc.parseError.linepos & vbNewline & _ "Source Text: " & xmlDoc.parseError.srcText & vbNewline & _ "Description: " & xmlDoc.parseError.reason getXMLError = strError end function xml.asp
It would be used as follows:
dim xmlDocument set xmlDocument = getXml("my_document.xml")
Let's say we have the following XML content (there is no doctype included):
<?xml version="1.0"?> <books> <book id="1"> <title>My Life</title> <author>John Smith</author> </book> <book id="2"> <title>How to Drive a Car</title> <author>Jake Miller</author> <description>Describes in great detail how to drive a car. </description> </book> </books> books.xml
...and we want to output something like this (just some XHTML headers and paragraphs):
My Life
John Smith
(No description available)
How to Drive a Car
Jake Miller
Describes in great detail how to drive a car.
...then we could use this:
set xmlDocument = getXml("books.xml") xPath = "//book" set books = xmlDocument.selectNodes(xPath) for each book in books bookTitle = book.selectSingleNode("title").text bookAuthor = book.selectSingleNode("author").text set bookDescriptionNode = _ book.selectSingleNode("description") if bookDescriptionNode is nothing then bookDescription = "(No description available)" else bookDescription = bookDescriptionNode.text end if xhtml = xhtml & "<h3>" & _ bookTitle & "<br />" & _ "<em>" & bookAuthor & "</em></h3>" xhtml = xhtml & "<p>" & bookDescription & "</p>" next response.write xhtml default.asp
Continue with XML Sitemap Creator