Examples: NotesDOMParser class

This agent parses the origXML file, walks the DOM node tree and creates a report in outputFile about the nodes encountered.

(Declarations)
Dim domParser As NotesDOMParser
Dim LF As String
Sub Initialize
  Dim session As NotesSession
  Dim db As NotesDatabase
  Dim inputStream As NotesStream, outputStream As NotesStream
  Dim docNode As NotesDOMDocumentNode
  
  Dim origXML As String, outputFile As String
  origXML = "c:\dxl\xmldom.xml"
  outputFile = "c:\dxl\DOMtree.txt"
  
  Dim header As String
  header = "Walk Tree agent"
  LF = Chr(13)+Chr(10)
  
  On Error Goto errh
  
  Set session = New NotesSession    
  Set db = session.CurrentDatabase
  
  'create the output file
  Set outputStream =session.CreateStream
  outputStream.Open (outputFile)
  outputStream.Truncate
  
  'write report title
  outputStream.WriteText ("DOM Parser Report - " )
  outputStream.WriteText (header+LF)
  
  'open the XML file
  Set inputStream = session.CreateStream
  inputStream.Open (origXML)
  If inputStream.Bytes = 0 Then
    outputStream.WriteText (origXML+" is empty"+LF)
    Goto results
  End If
  
  'create DOM parser and process
  Set domParser=session.CreateDOMParser(inputStream, outputStream)
  domParser.Process
  
  'get the document node
  Set docNode = domParser.Document
  
  Call walkTree(docNode)
  
results:
  Call outputStream.Close
  Exit Sub
errh:
  outputStream.WriteText ("errh: "+Cstr(Err)+":  "+Error+LF)
  Resume results
End Sub
Sub walkTree ( node As notesdomnode)
  Dim child As notesdomnode
  Dim elt As notesdomnode
  Dim attrs As notesdomnamednodemap
  Dim a As notesdomattributenode
  Dim piNode As Notesdomprocessinginstructionnode
  LF = Chr(13)+Chr(10)
  
  If Not node.IsNull Then  
    Select Case node.NodeType
    Case DOMNODETYPE_DOCUMENT_NODE:        ' If it is a Document node
      domParser.Output( "Document node: "+node.Nodename )
      Set child = node.FirstChild   ' Get the first node
      Dim numChildNodes As Integer
      numChildNodes = node.NumberOfChildNodes
      domParser.Output(" has "+Cstr(numChildNodes)+" Child Nodes"+LF)
      
      While numChildNodes > 0 
        Set child = child.NextSibling ' Get next node
        numChildNodes = numChildNodes - 1
        Call walkTree(child)
      Wend
      
    Case DOMNODETYPE_DOCUMENTTYPE_NODE:   ' It is a <!DOCTYPE> tag
      domParser.Output("Document Type node: "+ node.NodeName+LF)
      
    Case DOMNODETYPE_TEXT_NODE:           ' Plain text node
      domParser.Output("Text node: "+node.NodeValue+LF)
      
    Case DOMNODETYPE_ELEMENT_NODE:        ' Most nodes are Elements
      domParser.Output("Element node: "+node.NodeName )
      Set elt = node
      
      Dim numAttributes As Integer, numChildren As Integer
      numAttributes = elt.attributes.numberofentries
      domParser.Output(" has "+Cstr(numAttributes)+" Attributes"+LF)
      
      Set attrs = elt.Attributes     ' Get attributes
      
      Dim i As Integer
      For i = 1 To numAttributes     ' Loop through them
        Set a = attrs.GetItem(i)
        ' Print attr. name & value
        domParser.Output("Attribute "+a.NodeName+": "+a.NodeValue+LF)
      Next
      
      numChildren =  elt.NumberOfChildNodes
      Set child = elt.FirstChild     ' Get child
      While numChildren > 0
        Call walkTree(child)
        Set child = child.NextSibling   ' Get next child
        numChildren = numChildren - 1
      Wend
      domParser.Output( elt.nodeName+LF)
      
    Case DOMNODETYPE_COMMENT_NODE:                ' Comments
      domParser.Output("Comment node: "+node.NodeValue+LF)
      
    Case DOMNODETYPE_PROCESSINGINSTRUCTION_NODE:  ' Handle PI nodes
      Set piNode = node
      domParser.Output("Processing Instruction node: " )
      domParser.Output(" with Target  "+piNode.Target+_
      " and Data "+piNode.Data+LF)
      
    Case DOMNODETYPE_CDATASECTION_NODE:           ' CDATA sections
      domParser.Output("CDATA Section node: "+node.NodeName)
      domParser.Output(" has value of "+node.NodeValue+LF)
      
    Case DOMNODETYPE_ENTITYREFERENCE_NODE:        ' Handle entities
      domParser.Output("Entity Reference node: "+node.NodeName+LF)
      
    Case Else:
      domParser.Output("Ignoring node: "+Cstr(node.NodeType)+LF)
      
    End Select  'node.NodeType
  End If        'Not node.IsNull
End Sub