Manually serializing NotesDOMNodes

In my earlier post today, I described a problem I have, where I wanted to get the XML string of a DOM tree. I’ve started writing my own routine, this is what I have right now. It’s far from complete, since it can not handle namespaces, CDATA etc. yet:
This is a new version that handles siblings better, and not getting out of stack space 🙂


Public Sub serializeNode(node As NotesDOMNode_
, outStream As NotesStream)
On Error Goto catch

If node.IsNull Then Exit Sub

Dim currentNode As NotesDOMNode
Set currentNode = node

Do Until currentNode.isNull
Select Case currentNode.nodeType
Case DOMNODETYPE_DOCUMENT_NODE
Case DOMNODETYPE_XMLDECL_NODE
Dim xmlDecl As NotesDOMXMLDeclNode
Set xmlDecl = currentNode
Call outStream.WriteText("< ?xml version=""" _ + xmlDecl.version + """ encoding=""" + xmldecl.encoding + """?>")
Case DOMNODETYPE_ELEMENT_NODE
Call outStream.writeText("< " + currentNode.nodeName + "") Dim nodeMap As NotesDOMNamedNodeMap Set nodeMap = currentNode.attributes Dim attribute As NotesDOMNode Dim i As Integer For i = 1 To nodeMap.numberOfEntries Set attribute = nodeMap.getItem(i) Call outStream.WriteText(" " + attribute.nodeName + "=") Call outStream.WriteText("""" + attribute.NodeValue + """") Next i Call outStream.writeText(">")
Case DOMNODETYPE_TEXT_NODE
Call outStream.writeText(currentNode.NodeValue)
Case DOMNODETYPE_COMMENT_NODE
Call outStream.writeText("")
Case Else
Error 2000, "Unhandled node type [" _
& currentNode.nodeType & "]"
End Select

Dim endWritten As Boolean

If currentNode.hasChildNodes Then
Call serializenode(currentNode.firstChild, outStream)
End If

If currentNode.NodeType = DOMNODETYPE_ELEMENT_NODE Then
Call outStream.writeText("")
endWritten = True
End If

Set currentNode = currentNode.nextSibling
Loop

If node.NodeType = DOMNODETYPE_ELEMENT_NODE _
And Not endWritten Then
Call outStream.writeText("")
End If
Exit Sub
catch:
Error Err, Err & Lsi_Info(2) & ":" & Erl & "|" & Error
End Sub