Can I Create an DOM Tree in VBA without Loading One

R

Red Ryder

I have written a simple recursive parser in VBA (Word - Office 2003)
to parse the XML-like information out of a Word document.

Now I would like to store it as a DOM tree uing the XML-API provided.
An example would be as follows. Although I can walk through a DOM
created by the XMLDoc.Load method, I cannot add to it or create one
from scratch.

Can you suggest a simple example which creates a DOM tree using VBA.

Thanks

Sub listall()
Dim xmlDoc As New DOMDocument
Dim currNode As IXMLDOMNode, newNode As IXMLDOMNode, refNode As
IXMLDOMNode, _
rootNode As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList

Set rootNode = xmlDoc.documentElement
'
' Create a new element and insert it before the last child of the
top-level node.
'
Set newNode = xmlDoc.createNode(1, "FUBAR", "JUNK")
Set currNode = rootNode.InsertBefore(newNode, rootNode.LastChild)
dah, dah dah
 
S

Stephen Bullen

Hi Red,
I have written a simple recursive parser in VBA (Word - Office 2003)
to parse the XML-like information out of a Word document.

Now I would like to store it as a DOM tree uing the XML-API provided.
An example would be as follows. Although I can walk through a DOM
created by the XMLDoc.Load method, I cannot add to it or create one
from scratch.

Can you suggest a simple example which creates a DOM tree using VBA.

Here's an example taken from Ch23 of "Professional Excel Development",
due out in February:

Public Sub PostTimeEntriesToWebService()

Dim rngCell As Range
Dim rngTable As Range
Dim domXML As MSXML2.DOMDocument

Set rngTable = wksSheet.Range(gsRNG_BILLABLE_HOURS)

'Create a new XML document
Set domXML = New MSXML2.DOMDocument

'Create the root element <TimeSheet>
Set domXML.documentElement = _
NewElement(domXML, "TimeSheet")

With domXML.documentElement

'Add the <Consultant> element
With .appendChild(NewElement(domXML, "Consultant"))

'Add the Consultant's ID and Name elements and values
.appendChild(NewElement(domXML, "ID")) _
.nodeTypedValue = rngTable.Cells(1, 1).Value

.appendChild(NewElement(domXML, "Name")) _
.nodeTypedValue = wksSheet.Range("inpEmployee").Value
End With

'Add the WeekEnding element and value
.appendChild(NewElement(domXML, "WeekEnding")) _
.nodeTypedValue = Format( _
wksSheet.Range("inpWeekEnding").Value, "yyyy-mm-dd")

' Loop each entry in the time sheet and add it to the XML
For Each rngCell In rngTable

'Add a <BillableHours> element
With .appendChild(NewElement(domXML, "BillableHours"))

'Add the elements for a BillableHours record
.appendChild(NewElement(domXML, "DateWorked")) _
.nodeTypedValue = Format(_
rngCell.Offset(0, 1).Value, "yyyy-mm-dd")

.appendChild(NewElement(domXML, "ProjectID")) _
.nodeTypedValue = rngCell.Offset(0, 2).Value

.appendChild(NewElement(domXML, "ActivityID")) _
.nodeTypedValue = rngCell.Offset(0, 3).Value

.appendChild(NewElement(domXML, "Hours")) _
.nodeTypedValue = _
Trim$(Str$(rngCell.Offset(0, 4).Value))
End With
Next rngCell
End With

'etc.

End Sub


' Create a new element with our namespace
Private Function NewElement( _
ByRef domXML As MSXML2.DOMDocument, _
ByVal sElementName As String) As IXMLDOMNode

Const sNS As String = _
"http://www.BMSLtd.ie/PETRASWeb/TimeSheet"

Set NewElement = domXML.createNode(NODE_ELEMENT, _
sElementName, sNS)

End Function


Regards

Stephen Bullen
Microsoft MVP - Excel
www.oaltd.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top