1

Тема: Замена значения тега в XML файле

Вытягиваем  с определенного вида с документа с вложения xml файл.
Закидываем файл в поток, через CreateDOMParser  получаем объект NotesDOMDocumentNode.

Но не получается заменить значение тега E-doc....

Sub Click(Source As Button)
    Print "--- XML ---"
    mytext$ =""
    On Error Goto ErrH
    Dim session As New NotesSession
    Dim ws As New NotesUIWorkspace
    Dim db As NotesDatabase
    Dim view As NotesView, sview As NotesView
    Dim dc As NotesDocumentCollection
    Dim uidoc As NotesUIDocument, uidoc2 As NotesUIDocument
    Dim doc As NotesDocument, templdoc As NotesDocument
    Dim rtitem As NotesRichTextItem, rtitem2 As NotesRichTextItem
    Dim object As NotesEmbeddedObject, object2 As NotesEmbeddedObject
    Dim picklist As Variant
    Dim mes As String, mes2 As String
    Dim rtitemA As NotesRichTextItem
    
    Dim domParser As NotesDOMParser
    Dim docNode As NotesDOMDocumentNode
    Dim strm As NotesStream    
    Dim  outputStream As NotesStream
    Dim  outputFile As String
    
    Set strm = session.CreateStream() 
    Set outputStream =session.CreateStream()
    
    outputFile = "C:\XML\DOMtree.txt"
    outputStream.Open (outputFile)
    outputStream.Truncate
    
    If  Dir$ ("C:\XML",16) =""  Then
        Mkdir "C:\XML" 
    End If
    datapatch$ = "C:\XML\"
    
    Set db = session.CurrentDatabase
    Set sview = db.GetView("xml_templates")
    server = db.Server    
    Set doc = ws.CurrentDocument.Document
    Set uidoc=ws.CurrentDocument
    
    Set dc = ws.PickListCollection( _
    PICKLIST_CUSTOM, _
    False, _
    server, _
    "Promdoc\documents", _
    "xml_templates", _
    "Шаблоны XML", _
    "Выберите шаблон") 
    If dc.Count = 0 Then Messagebox("Выбор не сделан") : Exit Sub
    
    Set templdoc = dc.GetFirstDocument()
    'Print templdoc.GetItemValue("name")(0)
    
    If templdoc.HasEmbedded Then 
        Set rtitemA =  templdoc.GetFirstItem("attach")
        Forall obj In rtitemA.EmbeddedObjects
            'Print obj.Source 
            Call obj.ExtractFile( datapatch$ & obj.Source )
            
            Call strm.Open(datapatch$ & obj.Source, "UTF-8" )    ', "ASCII"
            'mytext$ =  strm.ReadText ()
            'Msgbox mytext$
            
' Вариант 1
            'Set DXLImporter = session.CreateDXLImporter( strm, db)
            'Call DXLImporter.process()
            
            ' Вариант 2
            Set domParser=session.CreateDOMParser(strm, outputStream)
            Call domParser.Serialize( )
            domParser.Process
            Set docNode = domParser.Document
            
        End Forall
    End If
    
    Call XMLTree(docNode, outputStream)
    
    Call domParser.Serialize( )
    domParser.Process
    
    Set docNode = domParser.Document
    Dim docList As NotesDOMNodeList
    Dim ENode As NotesDOMElementNode
    
    Set docList =    docNode.GetElementsByTagName("E-doc")
    ' Print Cstr(docList.NumberOfEntries)
    If docList.NumberOfEntries >0 Then
        Set ENode=docList.GetItem(1)
        Print "Тэг " &  ENode.TagName &  " Значение " &  ENode.FirstChild.NodeValue
    End If
'Set domParser=session.CreateDOMParser(docNode, outputStream)
    Call    outputStream.Close    
    Call strm.Close    
    Exit Sub
ErrH:
    Print     Err, Error  & { в строке } & Erl & {  ошибка  }    & Error(Err) 
End Sub
Sub XMLTree(docNode As NotesDOMNode, outputStream As NotesStream)
    On Error Goto ErrH
    NL = Chr(13)+Chr(10)
    If docNode.IsNull Then Exit Sub
    
    Dim ndnChild As NotesDOMNode
    Dim ndnElt As NotesDOMNode
    Dim ndnnmAttrs As NotesDOMNamedNodeMap
    Dim ndan As NotesDOMAttributeNode
    Dim numChildNodes As Integer, i As Integer
    Dim numAttributes As Integer, numChildren As Integer
    
    If docNode.NodeType = 1 Then   '  NodeType = 2 у атрибута
        outputStream.WriteText    "элемент: " & docNode.Nodename  & NL
        
        If    docNode.ParentNode.NodeName = "Container"     And docNode.Nodename = "E-doc"    Then
            
            docNode.FirstChild.NodeValue = "1111111111111111111111111"
            outputStream.WriteText    " ------------- " & NL
            Print  "В XMLTree "  &  docNode.Nodename  & docNode.FirstChild.NodeValue
        End If
    End If
    
    If docNode.HasChildNodes Then
        
        Set ndnChild = docNode.FirstChild 
        numChildNodes = docNode.NumberOfChildNodes
        'Print    "дочерних узлов: " &       Cstr(numChildNodes)
        
        While numChildNodes > 0
            Set ndnChild = ndnChild.NextSibling
            numChildNodes = numChildNodes - 1 
            Call XMLTree(ndnChild, outputStream)
        Wend
        
    End If
    
    'Dim eNode As NotesDOMElementNode
    'For i = 1 To documentList.NumberOfEntries
    '    Set    eNode =    documentList.GetItem(i)
    '    Print eNode.Nodename    
    'Next
    
    Exit Sub
ErrH:
    Print     Err, Error  & { в строке } & Erl & {  ошибка  }    & Error(Err) 
End Sub

Поделиться

2

Re: Замена значения тега в XML файле

Вообще 
domParser.Process  закидывает в объект domParser  типа NotesDOMParser  саму XML-ку в виде дом дерева.
Set docNode = domParser.Document - берет с домпарсера  Document

Поделиться