Unit 12: Customising Applications

Criteria

The functional specification (12a)
(AO 2, 3)


The design (12b)
(AO 1, 2, 3, 4)


Prototyping & testing (12b, c & d)
(AO 1, 2, 3, 4)


Documentation (12c)
(AO 1, 3)


Evaluation (12e)
(AO 4)

12b

The first prototype (version 1.0)

Graphical User Interface:

This form pulls data sermons.xml and processes it and outputs a file called latest-sermons.html.

Code:

Option Explicit

'20070320 D.S Hollands
'Latest sermons VBA form to valid XHTML via XML and XSLT - Version 1.0

'This code firstly creates a file called "saved.xml", then loads it and transforms it into
'xhtml then saves the output of the transformation as "latest-sermons.html"

Private Sub btnParse_Click()

 'Declaring vars and constants
 '================================================

 Const PATH As String = "E:\test\"
 Dim newXMLDoc As New MSXML2.DOMDocument
 Dim xslt As New MSXML2.XSLTemplate30
 Dim xslDoc As New MSXML2.FreeThreadedDOMDocument30
 Dim xmlDoc As New MSXML2.DOMDocument30
 Dim xslProc As IXSLProcessor '<-- only works with MSXML 3.0 or higher
 Dim xhtmldoc As New MSXML2.DOMDocument
 Dim Err '<-- declaring error var
 '================================================

 'Below dumps the text contained within the form's fields into a xml file in memory

 newXMLDoc.loadXML _
 "<?xml version='1.0' encoding='UTF-8'?>" + vbNewLine + _
 "<?xml-stylesheet type='text/xsl' href='latest-sermons.xsl'?>" + vbNewLine + _
 "<sermon>" + vbNewLine + _
 " <date>" & txtDatePreached.Text & "</date>" + vbNewLine + _
 " <morningTitle>" & txtMornTitle.Text & "</morningTitle>" + vbNewLine + _
 " <morningSummary>" & txtMornSummary.Text & "</morningSummary>" + vbNewLine + _
 " <morningURL>" & txtMornURL.Text & "</morningURL>" + vbNewLine + _
 " <eveningTitle>" & txtEveTitle.Text & "</eveningTitle>" + vbNewLine + _
 " <eveningSummary>" & txtEveSummary.Text & "</eveningSummary>" + vbNewLine + _
 " <eveningURL>" & txtEveURL.Text & "</eveningURL>" + vbNewLine + _
 "</sermon>" + vbNewLine

 newXMLDoc.Save (PATH + "latest-sermons.xml") '<-- saves the loaded XML

 'MSXML2.30 (MSXML 3.0) is used because lower versions do not have an IXSL Processor
 
 xslDoc.Load (PATH + "latest-sermons.xsl") '<-- loads the xsl ready for transformation

 If (xslDoc.parseError.errorCode <> 0) Then '<-- checks the xslt is valid
  Set Err = xslDoc.parseError
  MsgBox ("Error: " & Err.reason) '<-- explains error
 Else
  Set xslt.StyleSheet = xslDoc '<-- assigns xslDoc as the Stylesheet
  xmlDoc.Load (PATH + "latest-sermons.xml") '<-- loads the saved.xml file
 End If

 If (xmlDoc.parseError.errorCode <> 0) Then '<-- checks the xml is valid
  Set Err = xmlDoc.parseError
  MsgBox ("Error: " & Err.reason) '<-- explains error
 Else
  Set xslProc = xslt.createProcessor() '<-- initialises processor
  xslProc.input = xmlDoc '<-- inputs the valid xml file
  xslProc.transform '<-- transforms the xmlDoc using the xslDoc
  xhtmldoc.loadXML (xslProc.output) '<-- Loads the processors output
  xhtmldoc.Save (PATH + "latest-sermons.html") '<-- Saves the output as xhtml
  MsgBox ("Update complete! Files saved in " & PATH) '<-- Informs user of end
 End If

End Sub

' This routine fills the form with the current latest-sermon.xml details
Private Sub btnQueryNode_Click()

 'Declaring vars and constants
 '================================================

 Const PATH As String = "E:\test\"
 Dim oXMLDoc As New MSXML2.DOMDocument30 '<-- DOMDocument30 due to functionality
 Dim oNode As IXMLDOMNode
 Dim oNodes As IXMLDOMNodeList '<-- Assigns oNodes as a Node List (collection)
 Dim i As Integer
 Dim detailName As String
 Dim dName As String
 Dim dText As String
 '================================================

 ' Assigning necessary truths to XMLDocument
 oXMLDoc.async = False
 oXMLDoc.validateOnParse = False
 oXMLDoc.resolveExternals = False
 oXMLDoc.preserveWhiteSpace = True '<-- Keeps the white spaces inside the xml data

 ' Loading XML data from latest-sermons.xml file
 If oXMLDoc.Load(PATH + "latest-sermons.xml") = False Then
   MsgBox "Failed to load latest sermon xml data from file."
   Exit Sub
 End If

 ' Commence querying the sermon node-set ==========================================

 ' Navigate to and load contents of first sermon node as a node list (Xpath)
 Set oNodes = oXMLDoc.SelectNodes("//sermon/*")

 For i = 0 To oNodes.Length - 1 '<-- Loop until list ends

  ' Jumps the "sermon" node and goes straight to the "date" node
  ' then next time goes to the next node in the list until list ends

  Set oNode = oNodes.NextNode

  ' Checks that node name is not empty (error checking - maybe unnecessary??)
  If Not (oNode Is Nothing) Then
 
   'Makes coding less tedious - consider making function though
   dName = oNode.nodeName '<-- dName = detail name = node name
   dText = oNode.Text '<-- dText = detail text = text in node
 
   'Check what detail is and assign text to correct text box
   Select Case dName '<-- (nodeName)
    Case Is = "date"
     txtDatePreached.Text = dText '<-- Fills text box with node text
    Case Is = "morningTitle"
     txtMornTitle.Text = dText
    Case Is = "morningURL"
     txtMornURL.Text = dText
    Case Is = "morningSummary"
     txtMornSummary.Text = dText
    Case Is = "eveningTitle"
     txtEveTitle.Text = dText
    Case Is = "eveningURL"
     txtEveURL.Text = dText
    Case Is = "eveningSummary"
     txtEveSummary = dText
    End Select


  End If

 Next '<-- looping...

End Sub

Private Sub btnClearForm_Click() '<-- clear form of all data

 Const dText As String = "" '<-- emptyString

 txtDatePreached.Text = dText
 txtMornTitle.Text = dText
 txtMornURL.Text = dText
 txtMornSummary.Text = dText
 txtEveTitle.Text = dText
 txtEveURL.Text = dText
 txtEveSummary = dText

End Sub

Click next to continue...

« BACK | Back to top | NEXT »