例: NodeName, NodeType and NodeValue properties

次のエージェントは origXML ファイルを解析し、ノードの名前と値の情報を outputFile に書き込みます。

(Declarations)
Dim domParser As NotesDOMParser
Dim NL As String    'carriage return + line feed
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¥xmlnode.xml"
  outputFile = "c:¥dxl¥nodeName.txt"
  
  Dim message As String
  message = "DOM Parser Report - NodeName & NodeValue"
  NL = 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 (message+NL)
  
  'open the XML file
  Set inputStream = session.CreateStream
  inputStream.Open (origXML)
  If inputStream.Bytes = 0 Then
    message = origXML+" is empty"
    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 walkNameValue(docNode)
  message = "end of file: "+origXML 
  
results:
  outputStream.WriteText (message)
  Call outputStream.Close
  Exit Sub
errh:
  message = Cstr(Err)+":  "+Error+Chr(13)
  Resume results
End Sub
Sub walkNameValue ( node As NotesDOMNode)
  Dim child As NotesDOMNode
  Dim eNode As NotesDOMElementNode
  Dim piNode As NotesDOMProcessingInstructionNode
  Dim numChildNodes As Integer
  NL = Chr(13)+Chr(10)
  
  If Not node.IsNull Then
    domParser.Output( CstrNodeType(node)+NL)
    domParser.Output( "Node name: "+node.NodeName+NL)
    
    Select Case node.NodeType
      
    Case DOMNODETYPE_DOCUMENT_NODE:        ' The Document node
      domParser.Output( "Node value: null"+NL)
      
      Set child = node.FirstChild   ' Get the first node
      numChildNodes = node.NumberOfChildNodes
      domParser.Output("Node has "+Cstr(numChildNodes)+" Child Nodes"+NL)
      
      While numChildNodes > 0 
        Call walkNameValue(child)
        Set child = child.NextSibling ' Get next node
        numChildNodes = numChildNodes - 1
      Wend
      
    Case DOMNODETYPE_ELEMENT_NODE:          ' Most nodes are Elements
      domParser.Output( "Node value:  null"+NL)
      Set eNode = node
      domParser.Output( "Tag name: "+eNode.Tagname+NL)
     
      Set child = node.FirstChild   ' Get the first node
      numChildNodes = node.NumberOfChildNodes
      domParser.Output("Node has "+Cstr(numChildNodes)+" Child Nodes"+NL)
      
      While numChildNodes > 0 
        Call walkNameValue(child)
        Set child = child.NextSibling           ' Get next child
        numChildNodes = numChildNodes - 1
      Wend
      
    Case DOMNODETYPE_DOCUMENTTYPE_NODE:   ' It is a <!DOCTYPE> tag
      domParser.Output( "Node value: null"+NL)
      
    Case DOMNODETYPE_TEXT_NODE:           ' Plain text node
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_COMMENT_NODE:        ' Comments
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_PROCESSINGINSTRUCTION_NODE:   ' Handle PI nodes
      Set piNode = node
      domParser.Output(" with Target  "+piNode.Target +" and Data "+piNode.Data+NL)
      
    Case DOMNODETYPE_ENTITY_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_ENTITYREFERENCE_NODE:         ' Handle entities
      domParser.Output( "Node value: null"+NL)
      
    Case DOMNODETYPE_CDATASECTION_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_ATTRIBUTE_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_DOCUMENTFRAGMENT_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_NOTATION_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_XMLDECL_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case Else:
      domParser.Output("Ignoring node: "+Cstr(node.NodeType)+NL)
      
    End Select  'node.NodeType
  End If        'Not node.IsNull
End Sub

Function CstrNodeType (currentNode As NotesDOMNode) As String
  If Not currentNode.IsNull Then
    Select Case currentNode.NodeType
      
    Case DOMNODETYPE_ELEMENT_NODE:       '1
      CstrNodeType = "DOMNODETYPE_ELEMENT_NODE"
      
    Case DOMNODETYPE_ATTRIBUTE_NODE:     '2
      CstrNodeType = "DOMNODETYPE_ATTRIBUTE_NODE"
      
    Case DOMNODETYPE_TEXT_NODE:          '3
      CstrNodeType = "DOMNODETYPE_TEXT_NODE_NODE"
      
    Case DOMNODETYPE_CDATASECTION_NODE:  '4
      CstrNodeType = "DOMNODETYPE_CDATASECTION_NODE"
      
    Case DOMNODETYPE_ENTITYREFERENCE_NODE:'5
      CstrNodeType = "DOMNODETYPE_ENTITYREFERENCE_NODE"
      
    Case DOMNODETYPE_ENTITY_NODE:         '6
      CstrNodeType = "DOMNODETYPE_ENTITY_NODE"
      
    Case DOMNODETYPE_PROCESSINGINSTRUCTION_NODE:  '7
      CstrNodeType = "DOMNODETYPE_PROCESSINGINSTRUCTION_NODE"
      
    Case DOMNODETYPE_COMMENT_NODE:        '8
      CstrNodeType = "DOMNODETYPE_COMMENT_NODE"
      
    Case DOMNODETYPE_DOCUMENT_NODE:       '9
      CstrNodeType = "DOMNODETYPE_DOCUMENT_NODE"
      
    Case DOMNODETYPE_DOCUMENTTYPE_NODE:   '10
      CstrNodeType = "DOMNODETYPE_DOCUMENTTYPE_NODE"
      
    Case DOMNODETYPE_DOCUMENTFRAGMENT_NODE:      '11
      CstrNodeType = "DOMNODETYPE_DOCUMENTFRAGMENT_NODE"
      
    Case DOMNODETYPE_NOTATION_NODE:              '12
      CstrNodeType = "DOMNODETYPE_NOTATION_NODE"
      
    Case DOMNODETYPE_XMLDECL_NODE:               '13
      CstrNodeType = "DOMNODETYPE_XMLDECL_NODE"
    End Select
  Else
    CstrNodeType = "Null Node"
  End If
End Function