LOTUSSCRIPT/COM/OLE CLASSES


Examples: ParentNode property
This agent parses the origXML file and writes information about parent, sibling and child nodes to outputFile.

(Declarations)
Dim session As NotesSession
Dim inputStream As NotesStream, outputStream As NotesStream
Dim domParser As NotesDOMParser
Dim docNode As NotesDOMDocumentNode
Dim NL As String  'carriage return + line feed
Dim nodeCount(12) As Long

Sub Initialize
 Dim i As Integer
 Dim j As Long
 
 Dim origXML As String, outputFile As String
 origXML = "c:\dxl\NAB.xml"
 outputFile = "c:\dxl\DOMnodes.doc"
 
 REM your message here!
 Dim message As String
 message = "DOM Parser Report - Node Relationships"
 
 On Error Goto errh
 
 'open the XML input file and create the report file
 If  Not xmlCreate(origXML,outputFile,message) Then
   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
 
 REM report on parent/child/sibling nodes
 For i = 0 To 12
   nodeCount(i) = 0
 Next
 
 Call walktree (docNode)
 
 outputStream.WriteText (NL+NL+"Report status ---"+NL)
 For j = 0 To 12
   outputStream.WriteText (Cstr(nodeCount(j))+" "+CstrNodeType(j)+"s"+ NL)
 Next
 message = NL+"end of file: "+origXML
 
results:
 outputStream.WriteText (message)
 Call outputStream.Close
 Messagebox "Report written to "+outputFile
 Exit Sub
errh:
 message = Cstr(Err)+":  "+Error+Chr(13)
 Resume results
End Sub

Sub walkTree ( node As NotesDOMNode)
 Dim child As NotesDOMNode
 Dim numChildNodes As Integer
 Dim nName As String
 NL = Chr(13)+Chr(10)
 
 If Not node.IsNull Then
   nName = CstrNodeType(node.NodeType)
   domParser.Output( nName+" " )
   
   Select Case node.NodeType
     
   Case DOMNODETYPE_DOCUMENT_NODE:        ' The Document node
     domParser.Output("Name: "+node.Nodename+NL)
     
     If node.ParentNode.IsNull Then
       domParser.Output(nName+"s have no parent")
     Else
       Messagebox "Node cannot have a parent!!", ,nName+" error"
       Exit Sub
     End If
     
     If node.NextSibling.IsNull And node.PreviousSibling.IsNull Then
       domParser.Output(" and no siblings"+NL )
     Else
       Messagebox "Node cannot have a sibling!!", ,nName+" error"
       Exit Sub
     End If
     
   Case Else:
     domParser.Output("Name: "+node.Nodename+NL)
     
     If node.ParentNode.Isnull Then
       Messagebox "Node must have a parent!!", ,nName+" error"
       Exit Sub
     End If
     
     If node.NextSibling.IsNull And node.PreviousSibling.IsNull Then
       domParser.Output(" has no siblings" )
     Else
       domParser.Output(" has a sibling")
     End If
     
   End Select  'node.NodeType
   
   If node.HasChildNodes Then
     Set child = node.FirstChild
     
     numChildNodes = node.NumberOfChildNodes
   
     If node.NodeType =  DOMNODETYPE_DOCUMENT_NODE Then
       domParser.Output(" has "+Cstr(numChildNodes) )
     Else
       domParser.Output(" and "+Cstr(numChildNodes) )
     End If
     
     If  numChildNodes = 1 Then
       domParser.Output( " child"+NL)
     Else
       domParser.Output( " children"+NL)
     End If
     
     While numChildNodes > 0
       Call walkTree(child)
       Set child = child.NextSibling
       numChildNodes = numChildNodes - 1
     Wend
   Else
     domParser.Output(" and no children"+NL)
   End If      'node.HasChildNodes
 End If        'Not node.IsNull
End Sub

Function xmlCreate (inFile As String, outFile As String, info As String)
' Open the XML file and create the report file

 Dim db As NotesDatabase
 Set session = New NotesSession
 Set db = session.CurrentDatabase
 
   'create the output file
 Set outputStream =session.CreateStream
 outputStream.Open (outFile)
 outputStream.Truncate
 
   'write report title
 outputStream.WriteText (info+Chr(13)+Chr(10))
 
   'open the XML file
 Set inputStream = session.CreateStream
 inputStream.Open (inFile)
 If inputStream.Bytes = 0 Then
   info = "XML file "+ inFile+" is empty"
   xmlCreate = False
 Else
   xmlCreate = True
 End If
 
End Function



Function CstrNodeType (currentNode As Long) As String
'Given a node type, convert it to a descriptive string.

 nodeCount(currentNode) = nodeCount(currentNode)+1
 
 Select Case currentNode
   
 Case DOMNODETYPE_ELEMENT_NODE:       '1
   CstrNodeType = "ELEMENT_NODE"
   
 Case DOMNODETYPE_ATTRIBUTE_NODE:     '2
   CstrNodeType = "ATTRIBUTE_NODE"
   
 Case DOMNODETYPE_TEXT_NODE:                   '3
   CstrNodeType = "TEXT_NODE"
   
 Case DOMNODETYPE_CDATASECTION_NODE:           '4
   CstrNodeType = "CDATASECTION_NODE"
   
 Case DOMNODETYPE_ENTITYREFERENCE_NODE:        '5
   CstrNodeType = "ENTITYREFERENCE_NODE"
   
 Case DOMNODETYPE_ENTITY_NODE:                 '6
   CstrNodeType = "ENTITY_NODE"
   
 Case DOMNODETYPE_PROCESSINGINSTRUCTION_NODE:  '7
   CstrNodeType = "PROCESSINGINSTRUCTION_NODE"
   
 Case DOMNODETYPE_COMMENT_NODE:                '8
   CstrNodeType = "COMMENT_NODE"
   
 Case DOMNODETYPE_DOCUMENT_NODE:               '9
   CstrNodeType = "DOCUMENT_NODE"
   
 Case DOMNODETYPE_DOCUMENTTYPE_NODE:           '10
   CstrNodeType = "DOCUMENTTYPE_NODE"
   
 Case DOMNODETYPE_DOCUMENTFRAGMENT_NODE:       '11
   CstrNodeType = "DOCUMENTFRAGMENT_NODE"
   
 Case DOMNODETYPE_NOTATION_NODE:               '12
   CstrNodeType = "NOTATION_NODE"
   
 Case DOMNODETYPE_XMLDECL_NODE:                '13
   CstrNodeType = "XMLDECL_NODE"
 End Select
 
End Function

See Also