例: Create Node methods

次のエージェントは origXML ファイルを解析し、新規のノードを作成します。結果は、outputFile で報告されます。

(Declarations)
Dim session As NotesSession
Dim inputStream As NotesStream, outputStream As NotesStream
Dim domParser As NotesDOMParser
Dim docNode As NotesDOMDocumentNode
Dim docRootNode As NotesDOMNode
Dim NL As String            'carriage return + line feed
Dim cstrType(13) As String  'convert node type to descriptive string

Sub Initialize
  Dim origXML As String, outputFile As String
  origXML = "c:¥dxl¥xmlin.xml"
  outputFile = "c:¥dxl¥DOMcreatenode.txt"
  NL = Chr(13)+Chr(10)

  cstrType(DOMNODETYPE_ELEMENT_NODE) =  "ELEMENT_NODE"
  cstrType(DOMNODETYPE_ATTRIBUTE_NODE) = "ATTRIBUTE_NODE"
  cstrType(DOMNODETYPE_TEXT_NODE) =  "TEXT_NODE"
  cstrType(DOMNODETYPE_CDATASECTION_NODE) =  "CDATASECTION_NODE"
  cstrType(DOMNODETYPE_ENTITYREFERENCE_NODE) = _
  "ENTITYREFERENCE_NODE"
  cstrType(DOMNODETYPE_ENTITY_NODE) = "ENTITY_NODE"
  cstrType(DOMNODETYPE_PROCESSINGINSTRUCTION_NODE) = _
  "PROCESSINGINSTRUCTION_NODE"  
  cstrType(DOMNODETYPE_COMMENT_NODE) =  "COMMENT_NODE"
  cstrType(DOMNODETYPE_DOCUMENT_NODE) = "DOCUMENT_NODE"
  cstrType(DOMNODETYPE_DOCUMENTTYPE_NODE) = "DOCUMENTTYPE_NODE"
  cstrType(DOMNODETYPE_DOCUMENTFRAGMENT_NODE) = _
  "DOCUMENTFRAGMENT_NODE"
  cstrType(DOMNODETYPE_NOTATION_NODE) = "NOTATION_NODE"
  cstrType(DOMNODETYPE_XMLDECL_NODE) =  "XMLDECL_NODE"
  
  On Error Goto errh
  
  Set session = New NotesSession
  Set outputStream =session.CreateStream
  outputStream.Open (outputFile)
  outputStream.Truncate
  outputStream.WriteText("DOM Create Nodes Report - original tree data"+NL)
  Set inputStream = session.CreateStream
  inputStream.Open (origXML)
  If inputStream.Bytes = 0 Then
    Messagebox "XML file "+ origXML+" is empty"
    Goto closeReport
  End If
  
  Call  createNewNodes
  
closeReport:
  Call outputStream.Close
  Messagebox "Report written to "+outputFile
  Exit Sub
errh:
  Messagebox (Cstr(Err) + ":  " + Error + Chr(13))
  Resume closeReport
End Sub

Sub createNewNodes
  Dim cdataNode As NotesDOMCDATASectionNode
  Dim commentNode As NotesDOMCommentNode
  Dim textNode As NotesDOMTextNode
  Dim docFragNode As NotesDOMDocumentFragmentNode
  Dim newNode As NotesDOMElementNode
  Dim entityrefNode As NotesDOMEntityReferenceNode
  Dim piNode As NotesDOMProcessingInstructionNode
  
  Set domParser=session.CreateDOMParser(inputStream, outputStream)
  domParser.Process
  Set docNode = domParser.Document
  Set docRootNode = docNode.DocumentElement
  
  'report on the tree: create new nodes
  Call walkTree(docRootNode)
  
  'update the tree
  outputStream.WriteText(NL+"DOM Create Nodes Report - modified tree"+NL)
  
  Set commentNode = docNode.CreateCommentNode("created comment node")
  Call docRootNode.AppendChild(commentNode)
  
  Set cdataNode = docNode.CreateCDataSectionNode("some cdata stuff")
  Call docRootNode.AppendChild(cdataNode)
  
  Set textNode =  docNode.CreateTextNode("New text node using a docfragment")
  Set docFragNode = docNode.CreateDocumentFragmentNode
  Call docFragNode.AppendChild(textNode)
  Call docRootNode.AppendChild(docFragNode)
  
  Set newNode = docNode.CreateElementNode("NewElement")
  Call docRootNode.AppendChild(newNode)
  
  Set entityrefNode = docNode.CreateEntityReferenceNode("NewEntity")
  Call docRootNode.AppendChild(entityrefNode)
  
  Set piNode = docNode.CreateProcessingInstructionNode("xml", "version=1.0")
  Call docRootNode.AppendChild(piNode)
  
  'report on updated tree
  Call walkTree(docRootNode)
  
End Sub

Sub walkTree ( node As NotesDOMNode)
  Dim child As NotesDOMNode
  Dim eNode As NotesDOMElementNode
  Dim piNode As NotesDOMProcessingInstructionNode
  Dim nNodes As Integer
  NL = Chr(13)+Chr(10)
  
  If Not node.IsNull Then 'report on the node
    domParser.Output( cstrType(node.NodeType) + NL)
    domParser.Output( "Node name: "+node.NodeName+NL)
    
    Select Case node.NodeType
    'some nodes have more information to report on
      
    Case DOMNODETYPE_DOCUMENT_NODE:
      Set child = node.FirstChild
      nNodes = node.NumberOfChildNodes
      domParser.Output("Node has "+Cstr(nNodes) _
      + " Child Nodes"+NL)
      
      While nNodes > 0
        Call walkTree(child)
        Set child = child.NextSibling
        nNodes = nNodes - 1
      Wend
      
    Case DOMNODETYPE_ELEMENT_NODE:
      Set eNode = node
      
      Set child = node.FirstChild
      nNodes = node.NumberOfChildNodes
      domParser.Output("Node has "+Cstr(nNodes) _
      + " Child Nodes"+NL)
      
      While nNodes > 0
        Call walkTree(child)
        Set child = child.NextSibling
        nNodes = nNodes - 1
      Wend
      
    Case DOMNODETYPE_TEXT_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_COMMENT_NODE:
      domParser.Output( "Node value: "+node.NodeValue+NL)
      
    Case DOMNODETYPE_PROCESSINGINSTRUCTION_NODE:
      Set piNode = node
      domParser.Output("Target:  "+piNode.Target +NL)
      domParser.Output( "Data: "+piNode.Data+NL)
      
    Case DOMNODETYPE_ENTITY_NODE:
      domParser.Output( "Node value: "+node.NodeValue+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)
      
    End Select 'node.NodeType
  End If 'Not node.IsNull
End Sub