(Newbie) Hi, I need to create add an agent that exports document data that is defined in a view into a xml formatted file, whch is then saved to the local computer, I never worked with xml before so any pointers on were to start would be great, thanks
Subject: LotusScript to export Selected Documents in a View to XML files
This code will export any selected document in a view to an XML file. Each file is given a file name consisting of the document’s Note ID and a file extension of “xml”, and is written to H:\Notes6, which you can change as needed. To view these xml files, you will need to move them to a folder, which contains a subfolder, “xmlschemas”, with the style sheets (look in your Notes directory for this subfolder).
Create an agent called “XML Write”:
Declarations
Const kHTMLquote = {%22}
Const kHTMLAmp = {%26}
Const kHTMLApostrophe = {%27}
Const kHTMLlt = {%3C}
Const kHTMLgt = {%3E}
Dim session As NotesSession
Dim ws As NotesUIWorkspace
Dim dbSel As NotesDatabase
Dim sSrch As String
Dim sRplc As String
Dim XMLexp As NotesDXLExporter
Dim lDocs As Long
Dim lWritten As Long
Dim lInvalidDocs As Long
Dim EscChrs List As String
Sub Initialize()
On Error Goto ProcErr
Dim sMsg As String
If Not InitSession() Then Exit Sub
If Not WriteXMLSel() Then Exit Sub
If lWritten = 0 Then
sMsg = "No documents were written as XML."
Else
If lWritten = 1 Then
sMsg = "One document was written as XML to your H:\Notes6 Folder."
Else
sMsg = Format$(lWritten, "#,##0") + " documents were written as XML to your My Documents Folder."
End If
End If
Messagebox sMsg, 0, "Document XML Export to File Complete"
ExitProc:
Exit Sub
ProcErr:
Messagebox "Agent XML Search and Replace error in Initialize. Code: " + Cstr(Err()) + ": " + Error$() + " Line: " + Cstr(Erl()),_
16, "Application Error"
Resume ExitProc
End Sub
Function InitSession() As Integer
On Error Goto ProcErr
InitSession = False
Set session = New NotesSession
Set dbSel = session.CurrentDatabase
Set ws = New NotesUIWorkspace
lDocs = 0
lWritten = 0
lInvalidDocs = 0
EscChrs({"}) = {%22}
EscChrs({&}) = {%26}
EscChrs({<}) = {%3C}
EscChrs({>}) = {%3E}
InitSession = True
ExitProc:
Exit Function
ProcErr:
Messagebox "Agent XML Write error in InitSession. Code: " + Cstr(Err()) + " - " + Error$() + " Line: " + Cstr(Erl()),_
16, "Application Error"
Resume ExitProc
End Function
Function IsDocValid(docChk As NotesDocument, iIsValid As Integer) As Integer
On Error Goto ProcErr
Dim itemSel As NotesItem, vVals As Variant
IsDocValid = False
iIsValid = False
If Not docChk Is Nothing Then
If docChk.IsValid Then
If Not docChk.IsDeleted Then
Set itemSel = docChk.GetFirstItem("Form")
If Not itemSel Is Nothing Then
vVals = itemSel.Values
If Datatype(vVals) > 1 Then
If Isarray(vVals) Then
iIsValid = True
End If
End If
End If
End If
End If
End If
IsDocValid = True
ExitProc:
Exit Function
ProcErr:
Messagebox "Agent XML Write error in IsDocValid. Code: " + Cstr(Err()) + " - " + Error$() + " Line: " + Cstr(Erl()),_
16, "Application Error"
Resume ExitProc
End Function
Function WriteXMLSel() As Integer
On Error Goto ProcErr
Dim dcSel As NotesDocumentCollection, docSel As NotesDocument, docSelNext As NotesDocument
Dim sFN As String, streamXML As NotesStream, lDoc As Long
Dim iDocIsValid As Integer
Dim sErrMsg As String
Dim sTotalDocs As String
WriteXMLSel = False
lDoc = 0
Set dcSel = dbSel.UnprocessedDocuments
lDocs = dcSel.Count
sTotalDocs = " of " + Format$(lDocs, "#,##0") + ".."
Set docSel = dcSel.GetFirstDocument()
Do Until docSel Is Nothing
lDoc = lDoc + 1
Print "Processing document " + Format$(lDoc, "#,##0") + sTotalDocs
Set docSelNext = dcSel.GetNextDocument(docSel)
'Verify document's validity
If Not IsDocValid(docSel, iDocIsValid) Then Exit Function
If iDocIsValid Then
Set streamXML = Nothing
Set streamXML = session.CreateStream()
If Not streamXML.Open("H:\Notes6\" + docSel.NoteID + ".xml") Then
Messagebox "Unable to open a new file in your H:\Notes6 Folder", 0, "XML Write"
Exit Function
End If
Set XMLexp = Nothing
Set XMLexp = session.CreateDXLExporter(docSel, streamXML)
Call XMLexp.Process()
Call streamXML.Close()
lWritten = lWritten + 1
Else 'Invalid document
lInvalidDocs = lInvalidDocs + 1
End If 'Valid Document
Set docSel = docSelNext
Loop
WriteXMLSel = True
ExitProc:
Exit Function
ProcErr:
sErrMsg = "Agent XML Write error in WriteXMLSel. Code: " + Cstr(Err()) + " - " + Error$() + " Line: " + Cstr(Erl())
Messagebox sErrMsg, 16, "Application Error"
Resume ExitProc
End Function
Subject: RE: LotusScript to export Selected Documents in a View to XML files
Hi James, thanks for your prompt response, this works great, however I need it to export only defined data and not all fields from each document, is there anyway to do that ?
Subject: RE: LotusScript to export Selected Documents in a View to XML files
Take a look at this posting to create XML on the fly: http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/d39ed42505abb489852571f100232a6a?OpenDocument
Otherwise, you will have to use the NoteDOMParser to delete unwanted nodes prior to export, or you will have to use the NotesSAXParser to transform the XML by outputing only the desired data.