Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Dynamic HTML display from your eVB application using XML/XSL

Written by Jason Hawgood  [author's bio]  [read 56261 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

Public Function GetXMLByHTTP(URL As String) As String
Dim loXMLHTTP As XMLHTTPRequest

' Create XML HTTP object
Set loXMLHTTP = CreateObject("Microsoft.XMLHTTP")

' Open URL to Listener
loXMLHTTP.Open "GET", URL, False, "", ""

' Set header info
loXMLHTTP.setRequestHeader "Content-Type", "text/xml"

' Make request
loXMLHTTP.send

'Return the XML response
GetXMLByHTTP = loXMLHTTP.responseText

'Clean up
Set loXMLHTTP = Nothing
End Function

Enter Extensible Stylesheet Language (XSL)

Now that we have an XML document loaded into a XMLDOM object, we will need to transform our XML data into HTML using XSL (Extensible Stylesheet Language). HTML output is accomplished via a style sheet, which parses the XML file for matching patterns using the XPATH language. Here is a snippet of the XSL used to transform our XML document. You will need to change the path to the image in the XSL document to the correct image location, by default any emulation app are run in the \Windows\Start Up\ directory.

[SNIPPET of Emplyees.XSL]

To use the XSL we will need a XMLDOM object to load the XSL stylesheet.

Set gXSLDoc = CreateObject("Microsoft.XMLDOM")Set gXSLDoc = LoadXMLFromFile(App.Path & "Employees.xsl")

You will notice we load the XSL document from a file. This is normally easy to do, however eVB has a problem loading XML from a URL, so we have to use a “work around”. Load the XSL file using the file control object and the LoadXMLFromFile function.

Public Function LoadXMLFromFile(XMLPath As String) As MSXML.DOMDocument

Dim XML As MSXML.DOMDocument
Set XML = CreateObject("Microsoft.XMLDOM")
' XML.Load(URL)
' This would be the easiest way to load the file into the parser, but...
' Returns an "Access Denied" error in the emulator and on a device
' Instead, open the XML file with the file control and store the contents in a string
Dim sFileXML, oFileXML
Set oFileXML = CreateObject("FileCtl.File")
oFileXML.Open XMLPath, fsModeInput, fsAccessRead, fsLockRead
sFileXML = oFileXML.Input(oFileXML.LOF)
oFileXML.Close
Set oFileXML = Nothing

' Return control after the whole string has been loaded
XML.async = False

' Load the XML string into the parser
XML.loadXML sFileXML

If XML.parseError.errorCode = 0 Then
Set LoadXMLFromFile = XML
Else
MsgBox XML.parseError.reason
End If

'Clean up
Set XML = Nothing

End Function

The gXMLDoc and gXSLDoc object variable are now set up for the transformation into HTML. We will use the transformNode method, exposed from the XMLDOM object to apply the style sheet to the XML. The HTML result of the transformations is derived from the BuildDirectory method called in the Form_Load event. Results of the transformation are stored in the global variable gsHTML.

gsHTML = xmldoc.transformNode(xsldoc.documentElement)

Displaying HTML in eVB

HTML is going to be displayed into the WebBrowserCE control. The BuildDirectory subroutine demonstrates how we display our global HTML string to the WebBrowserCE control after retrieval and transformation.

Public Sub BuildDirectory()

'Build the HTML to Render
Set gXMLDoc = CreateObject("Microsoft.XMLDOM")

'NOTE: Comment out this line if you do not have SQL Server XML support
gXMLDoc.loadXML GetXMLByHTTP(gURL)

'NOTE: If you do not have SQL Server XML support use this call instead
'Set gXMLDoc = LoadXMLFromFile(App.Path & "\Employees.xml")

'Transform the XML Set gXSLDoc = CreateObject("Microsoft.XMLDOM")
Set gXSLDoc = LoadXMLFromFile(App.Path & "\Employees.xsl")

gsHTML = gXMLDoc.transformNode(gXSLDoc.documentElement)
'Render the HTML to the WebBrowserCE control

objHtml.SetText (gsHTML)
objHtml.FitToWindow = True

End Sub

Previous Page  Next Page