Friday 1 April 2011

XML - Part Two - The Export

This is the second XML article in this mini series.  Once you have created the script library in your database (Part One), you can create the first of the two agents.

The agent below allows a user to export the selected documents as XML.  This creates an XML tree of "Documents", with each "Document" being a node.  This code fixes the file destination to "C:\Temp\Document.xml", so you will have to change this to fit your requirements.

I use a view to handle the field names to export, as I do not want to export every field.  My field configuration will be the subject of a further post.  For now, think of it as a list of field names where there is one document per field.
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 vwDoc As NotesView
Dim docExport As notesdocument
Dim docField As NotesDocument
Dim dc As NotesDocumentCollection
Dim dcExport As NotesDocumentCollection
Dim rtitem As NotesRichTextItem
Dim object As NotesEmbeddedObject
Dim i As Integer

Set db = s.CurrentDatabase

Dim xml As XMLProcessor
Dim docNode As NotesDOMElementNode
Set xml = New XMLProcessor("Documents")

Set vw = db.GetView( "VeSFieldsByForm" )
strKey = "Request"
Set dc = vw.GetAllDocumentsByKey(strKey, False)
Set dcExport = db.UnprocessedDocuments
Print "doc count = " & dcExport.Count
Set docExport = dcExport.GetFirstDocument()
i = 0
While Not docExport is Nothing
i=i+1
Print i
Set docNode = xml.appendElementNode(Nothing, "Document", "", "") 'if parent node is Nothing then root node will be used as parent node
Set docField = dc.Getfirstdocument()
While Not docField Is Nothing
strField= docField.FieldName(0)
Call xml.appendElementNode(docNode, strField, docExport.GetItemValue( strField )(0), "")
Set docField = dc.Getnextdocument(docField)
Wend
Set docExport = dcExport.Getnextdocument(docExport)
Wend
Print "Writing To file"
Call xml.toFile("c:\temp\Document.xml")

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

2 comments:

  1. Got an Error on the Use "XML" part. It's an Option Declare, but the agent says it's a option public and then i got an error.

    I cant skip it, and i can't have it in Option Public, please help me..

    ReplyDelete
    Replies
    1. XML is what he called the script library. What ever you called the part one script library is what you need to insert here.

      Delete