Friday 1 April 2011

XML - Part three - Import

This is the third XML article in this mini series.  Please take a look at Part One, which talks about the Script Library and Part Two, which talks about the export agent, if you have not already seen these.

The second agent I have written allows a user to import an XML file and replace all of the document fields with the values found in the XML document.

My code takes the first "Document" from the "Documents" tree and finds the <id> value, which is used as a key to find the document.  With the <id>, I can find the document and then run through all of the fields in the XML and replace the values within the document.  The field name is the same in the document as it is in the XML file, so no mapping is required in this example.
(I do confess I have used a "GoTo", which I try to avoid.  Sorry !)

Option Public
Option Declare

Use "XML"
Sub Initialize()
'On Error GoTo error_handle
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim s As New NotesSession
Dim ag As NotesAgent
Dim noteid As String, strKey As String, strForm As String,strField As String, strFilename As String
Dim vw As NotesView
Dim vwImport As NotesView
Dim vwDoc As NotesView
Dim docImport As notesdocument
Dim docField As NotesDocument
Dim dc As NotesDocumentCollection
Dim dcExport As NotesDocumentCollection
Dim rtitem As NotesRichTextItem
Dim object As NotesEmbeddedObject
Dim item As NotesItem
Dim intImport As Integer
Dim strValue As string

Set db = s.CurrentDatabase

Dim xml As XMLProcessor
Dim node As NotesDOMElementNode
Set xml = New XMLProcessor("Documents")
Call xml.parseFile("c:\temp\Document.xml")

Set vwImport = db.GetView( "(vwLkpByID)" )
Set dc = vw.GetAllDocumentsByKey("Request", False)

intImport=0

BeginImport:
intImport=intImport+1
Set node = xml.selectNode(Nothing, "Document:" & CStr(IntImport) & ">id")
If Not node Is Nothing Then
strKey = xml.getNodeValue(node, "id")
Set docImport = vwImport.getdocumentbykey(strKey)
If Not docImport is Nothing then
Set docField = dc.Getfirstdocument()
While Not docField Is Nothing
strField= docField.FieldName(0)
If strField <> "id" Then
Set node = xml.selectNode(Nothing, "Document:" & CStr(IntImport) & ">" & strField)
strValue = xml.getNodeValue(node,"-" )
Set item = docImport.ReplaceItemValue(strField, strValue )
End If
Set docField = dc.Getnextdocument(docField)
Wend
Call docImport.Save( True, True )
End If
GoTo BeginImport
End If


ex_sub:
Exit Sub

error_handle:
On Error Resume Next
Print "XML Document: " + Error$ + ". Line: " + Str$(Erl())
doc.errorlog = "XML Document: " + Error$ + ". Line: " + Str$(Erl())
Call doc.Computewithform(True, False)
Call doc.Save(True,True)
'Call SendError( "ProcessStepNonUI: " + Error$ + ". Line: " + Str$(Erl()) + " " + doc.Number(0) + " " + doc.flow_step_name(0) + " " + Ok_Reject  + " Server: " + db.Server )
Resume ex_sub
End Sub

No comments:

Post a Comment