Front End XML For ASP Developers - Configuration Files
(Page 3 of 5 )
As we see with most commercially available systems today, XML has become the format of choice for application configuration files. Take, for example, products like BEA WebLogic or the Microsoft .NET framework – and you will find that both system-level and application-specific configuration data is stored in an XML format.
Along the same lines, we now see XML used for formatting configuration files in custom-built internal Web and client/server applications, often even replacing such widely used standard as ".ini" files.
Why XML then? Simple. Because it's both easy to read by humans -- and thanks to standard libraries available in all popular languages -- easy to parse within the code.
A good example of this happening is a situation when you need to persist your settings in an ASP application. In this case, using global.asa just isn't good enough – application variables expire because of inactivity, and the data is lost. Let's take a look at an example when XML is used to build a typical configuration file and persist the server hit count tally.
The XML file itself looks very simple:
<?xml version="1.0"?>
<Application version="2.0">
<HitCount>5</HitCount>
</Application>The key to implementing this logic is to load the data into application variables in order to make it available on all pages across the application. This can be accomplished in the Application_onStart procedure of the global.asa file:
Sub Application_OnStart
Dim objXmlDom, xmlRoot
Set objXmlDom = CreateObject("Microsoft.XMLDOM")
objXmlDom.async = False
objXmlDom.Load Server.MapPath(".") & "\config.xml"
Set xmlRoot = objXmlDom.documentElement
Application("HitCount") = CInt(xmlRoot.childNodes(0).text)
Set xmlRoot = Nothing
Set objXmlDom = Nothing
End SubThen, in any page of the application, we can use standard ASP syntax to update the HitCount Application variable:
Application.Lock
Application("HitCount") = Application("HitCount") + 1
Application.UnLockThe last piece of the puzzle is making sure that the value of the HitCount variable is saved to the XML file before IIS is unloaded from memory. This can be done in Application_OnEnd procedure of global.asa file, as shown below:
Sub Application_OnEnd
Dim objXmlDom, xmlRoot
Set objXmlDom = CreateObject("Microsoft.XMLDOM")
objXmlDom.async = False
objXmlDom.Load Server.MapPath(".") & "\config.xml"
Set xmlRoot = objXmlDom.documentElement
xmlRoot.childNodes(0).text = Application("HitCount")
objXmlDom.Save Server.MapPath(".") & "\config.xml"
Set xmlRoot = Nothing
Set objXmlDom = Nothing
End SubFormatting XML for presentationIn most systems today the key to information processing is its immediate availability over the web. So instead of parsing XML data and storing it in a temporary location (be it in memory, structured database or files on the hard drive), XSL templates are used to format XML data into HTML and present it to web clients right there and then.
XSL Stands For eXtensible Style Sheet LanguageAn XSL document is an XML-based template, which is used by an XML parser to format XML data into HTML documents. XSL syntax allows for such programming structures as loops and conditional statements, but is mostly geared towards formatting data for presentation rather than building business logic.
For example, the owner of a Yahoo store may need to review all orders before uploading them to database. Given XML in the earlier code example, it would be very easy to create an XSL style sheet that converts the XML into one big HTML table.
Keep in mind, the parser components still needs to be called in order to accomplish this task. Note that both XML and XSL files must first be loaded into parser components, and only then can the style sheet can be applied. For example:
Set objXml = Server.CreateObject("Microsoft.XMLDOM")
objXml.async = false
objXml.load(Server.MapPath(".") & "\YahooOrders.xml")
Set objXsl = Server.CreateObject("Microsoft.XMLDOM")
objXsl.async = false
objXsl.load(Server.MapPath(".") & "\YahooOrders.xsl")
'-- Apply stylesheet now
Response.Write objXml.transformNode(objXsl)The reason for this structure is the case when one XML file can be formatted using different style sheets depending on user preferences. This facilitates the key concept of separating data and presentation layers of the application.
Next: There's a Bonus >>
More ASP Articles
More By Eugene Gilerson