Subject: RE: Help on “XML → Notes” troubles
Fun stuff…In designer help lookup: NotesDOMDocumentNode
and a function called WalkTree.
You could also just use the SaxParser if you just need to grab the contents of the xml and not do any transformations to the xml like adding nodes, or XSLT’ing it into another format:
Here’s an example of a LotusScript agent that takes a document and rips through the DXL to produce:
words words words etc.
Every start element that is hit, the agent writes it down and does something,
Every end element the agent finishes something,
Make a Lotus Script Agent;
Put this in the DECLARATIONS:
And remove the labels (they will be red in the Declarations, they are just demarkation for this posting…)
Dim content() As String 'holds the output content in an array
Dim state List As String 'each element will have a state setting
Dim key As String 'key will hold the current state setting
Dim textbufffer As String
Dim session As NotesSession
Dim agentLog As NotesLog
'Dim whatState As Variant
Dim stateIndex As Integer
Dim charactercount As Integer
Dim characterlimit As Integer
Dim paramlist List As String
Dim AttributeList List As String
Dim HashList List As String
Dim Attribute As Variant
Dim doc As notesdocument
Dim i As Integer
Dim theVariant As Variant
%INCLUDE “lsconst.lss”
INITIALIZE:
Sub Initialize
On Error Goto initializeError
Set session = New NotesSession
Set agentLog = New NotesLog("Agent log")
Call agentLog.OpenAgentLog
agentLog.LogActions = True
Dim doc As NotesDocument
Set doc = session.DocumentContext
’ Call makeParams()
Dim threadsview As NotesView
Set threadsview = session.CurrentDatabase.getView("View")
Dim dc As NotesDocumentCollection
Set dc = threadsview.GetAllDocumentsByKey("513D1BC76A68838AEE1584EDD9EC7648")
characterlimit = 1000
charactercount=-1
Dim saxParser As NotesSAXParser
Dim exporter As NotesDXLExporter
Set exporter = session.CreateDXLExporter(dc)
exporter.OutputDOCTYPE = False
Dim xml_out As NotesStream
Set xml_out=session.CreateStream
’ Call xml_out.WriteText(“Content-Type: text/text”)
Dim filename As String
’ filename$ = "c:" + Left(session.CurrentDatabase.FileName, Len(session.CurrentDatabase.FileName) - 4) + “_sax.xml”
’ If Not xml_out.Open(filename$) Then
’ agentLog.LogAction( "Cannot open " + filename$ + “. Check to make sure this directory exists.”)
’ Exit Sub
’ End If
Call xml_out.Truncate
Set saxParser=session.CreateSAXParser(exporter , xml_out)
On Event SAX_StartDocument From saxParser Call SAXStartDocument
On Event SAX_EndDocument From saxParser Call SAXEndDocument
On Event SAX_StartElement From saxParser Call SAXStartElement
On Event SAX_EndElement From saxParser Call SAXEndElement
On Event SAX_Characters From saxParser Call SAXCharacters
On Event SAX_IgnorableWhitespace From saxParser Call SAXIgnorableWhitespace
On Event SAX_NotationDecl From saxParser Call SAXNotationDecl
On Event SAX_ProcessingInstruction From saxParser Call SAXProcessingInstruction
On Event SAX_UnparsedEntityDecl From saxParser Call SAXUnparsedEntityDecl
On Event SAX_Warning From saxParser Call SAXWarning
On Event SAX_Error From saxParser Call SAXError
On Event SAX_FatalError From saxParser Call SAXFatalError
exporter.Process ' initiate parsing
’ Print “begin”
Print ("Content-Type: text/html")
Print xml_out.ReadText()
’ Do
’ buffer = inStream.Read(32767)
’ Call outStream.Write(buffer)
’ Loop Until inStream.IsEOS
Exit Sub
initializeError:
agentLog.LogAction( "handleInitializeError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :handleInitializeError")
Resume Next
End Sub
SAXSTARTELEMENT:
Sub _
SAXStartElement ( _
Source As Notessaxparser, _
Byval elementname As String, _
Attributes As NotesSaxAttributeList)
On Error Goto SAXStartElementError
If Not Iselement(state(elementname)) Then state(elementname)=0
’ Attribute = getAttributes(Attributes)
’ Source.Output(“<” & elementname & “>”)
’ Call getAttributes(Attributes)
’ Forall atts In AttributeList
’ Source.Output(Listtag(atts) & " = " & atts & Chr(10))
’ End Forall
‘’ If Attributes.Length > 0 Then
’ Dim attrname As String
’ For i = 1 To Attributes.Length
’ Source.Output(attributes.GetName(i) & " = " & attributes.GetValue(attributes.GetName(i)) & Chr(10))
’ Next
’ End If
Select Case elementname
Case "noteinfo"
'Messagebox "Start " & elementname
Call getAttributes(Attributes)
state(elementname)=AttributeList("noteid")
key = elementname
’ Source.Output({
})
Erase AttributeList
Case "item"
'Messagebox "Start " & elementname
Call getAttributes(Attributes)
'Messagebox "Name is " & AttributeList("name")
If AttributeList("name") = "discussionbody" Then
'Messagebox "Start " & elementname
state(AttributeList("name"))=1
key = AttributeList("name")
End If
Erase AttributeList
Case "richtext"
'Messagebox "Start " & elementname
state(elementname)=1
key = elementname
Case "par"
'Messagebox "Start " & elementname
If state("richtext") And state("discussionbody") Then
'Messagebox "Start discussionbody"
state(elementname)=1
key = elementname
If charactercount= -1 Then
charactercount = characterlimit
theVariant = {<div id="} & state("noteinfo") & {" class="popup" style="display=block;">} & Chr(10)
HashList("head") = theVariant
HashList("body")=""
’ Source.Output({<div id=“} & state(“noteinfo”) & {” class=“popup” style=“display=none;”>})
End If
End If
Case Else
End Select
Exit Sub
SAXStartElementError:
agentLog.LogAction( "SAXStartElementError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :SAXStartElementError")
Resume Next
End Sub
SAXENDELEMENT:
Sub _
SAXEndElement ( _
Source As Notessaxparser, _
Byval ElementName As String)
On Error Goto SAXEndElementError
’ Source.Output(“</” & elementname & “>”)
Select Case elementname
Case "noteinfo"
'Messagebox "End " & elementname
Case "item"
'Messagebox "End " & elementname
Case "richtext"
'Messagebox "End " & elementname
'This happens when the par's run out before characterlimit
If state("richtext") And state("discussionbody") Then
If HashList("body")<>"" Then
Source.Output(HashList("head") & HashList("body") & Chr(10) & {</div>} & Chr(10))
End If
Erase HashList
state(elementname)=0
state("discussionbody")=0
charactercount=-1
End If
Case "par"
'Messagebox "End " & elementname
'This only happens when the characterlimit# of characters are printed
If state("richtext") And state("discussionbody") And charactercount=-1 Then
'Messagebox "End rt state " & state("richtext") & " discbody is " & state("DiscussionBody") & " charcount is " & charactercount
If HashList("body")<>"" Then
Source.Output(HashList("head") & HashList("body") & Chr(10) & {</div>} & Chr(10))
End If
Erase HashList
state(elementname)=0
state("richtext")=0
state("discussionbody")=0
End If
Case Else
End Select
Exit Sub
SAXEndElementError:
agentLog.LogAction( "SAXEndElementError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :SAXEndElementError")
Resume Next
End Sub
SAXCHARACTERS:
Sub _
SAXCharacters ( _
Source As Notessaxparser, _
Byval Characters As String, _
Count As Long)
On Error Goto SAXCharactersError
’ If Characters <> Chr(10) Then
’ Source.Output(Characters)
’ End If
Select Case key
Case "noteinfo"
Case "item"
Case "richtext"
Case "par"
'Messagebox "Start " & key
If state("richtext") And state("discussionbody") Then
'Messagebox "rt state is " & state("richtext") & " discbody state is " & state("DiscussionBody")
If (charactercount-count) >0 Then
charactercount = charactercount - count
theVariant = Left(Characters , count)
HashList("body")=HashList("body") & theVariant
’ Source.Output (Left(Characters , count))
'Messagebox Left(Characters , count)
Else
theVariant = Left(Characters , charactercount)
HashList("body")=HashList("body") & theVariant
’ Source.Output (Left(Characters , charactercount))
If HashList("body")<>"" Then
Source.Output(HashList("head") & HashList("body") & Chr(10) & {</div>} & Chr(10) )
End If
charactercount = -1
state("richtext") = 0
state("discussionbody") = 0
key=""
End If
End If
Case Else
End Select
Exit Sub
SAXCharactersError:
agentLog.LogAction( "SAXCharactersError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :SAXCharactersError")
Resume Next
End Sub
SAXIGNORABLEWHITESPACE:
Sub _
SAXIgnorableWhitespace ( _
Source As Notessaxparser,_
Byval characters As String, _
Count As Long)
'Messagebox "Ignorable Whitespace found", MB_ICONINFORMATION
End Sub
SAXNOTATIONDECL:
Sub _
SAXNotationDecl ( _
Source As Notessaxparser, _
Byval NotationName As String, _
Byval publicid As String, _
Byval systemid As String)
'Messagebox "Notation Declaration found", MB_ICONINFORMATION
End Sub
SAXPROCESSINGINSTRUCTION:
Sub _
SAXProcessingInstruction ( _
Source As Notessaxparser,_
Byval target As String, _
Byval PIData As String)
'Messagebox "Processing Instruction found", MB_ICONINFORMATION
End Sub
SAXUNPARSEDENTITYDECL:
Sub _
SAXUnParsedEntityDecl ( _
Source As Notessaxparser,_
Byval Entityname As String, _
Byval publicid As String,_
Byval systemid As String, _
Byval notationname As String)
'Messagebox "Unparsed Entity Declaration found", MB_ICONINFORMATION
End Sub
SAXWARNING:
Sub _
SAXWarning ( _
Source As Notessaxparser, _
Exception As NotesSaxException)
On Error Goto SAXWarningError
'Messagebox "Warning - "+Exception.Message, MB_ICONINFORMATION
Print("Warning - "+Exception.Message)
Exit Sub
SAXWarningError:
agentLog.LogAction( "SAXWarningError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :SAXWarningError")
Resume Next
End Sub
SAXERROR:
Sub _
SAXError ( _
Source As Notessaxparser, _
Exception As NotesSaxException )
On Error Goto SAXErrorError
'Messagebox "Error - "+Exception.Message, MB_ICONINFORMATION
Print("Error - "+Exception.Message)
Exit Sub
SAXErrorError:
agentLog.LogAction( "SAXErrorError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :SAXErrorError")
Resume Next
End Sub
SAXFATALERROR:
Sub _
SAXFatalError ( _
Source As Notessaxparser, _
Exception As NotesSaxException)
On Error Goto SAXFatalErrorError
'Messagebox "FatalError - "+Exception.Message, MB_ICONINFORMATION
Print("FatalError - "+Exception.Message)
Exit Sub
SAXFatalErrorError:
agentLog.LogAction( "SAXFatalErrorError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :SAXFatalErrorError")
Resume Next
End Sub
SAXSTARTDOCUMENT:
Sub SAXStartDocument ( Source As Notessaxparser)
'Messagebox "Start reading Document", MB_ICONINFORMATION
agentLog.LogAction( "Start reading Document")
End Sub
SAXENDDOCUMENT:
Sub _
SAXEndDocument ( _
Source As Notessaxparser)
’ Erase state
'Messagebox "End of Document", MB_ICONINFORMATION
End Sub
GETATTRIBUTES:
Function getAttributes(attributes As NotesSAXAttributeList) As Variant
On Error Goto getAttributesError
Dim arg As Variant
Dim parts As Variant
Dim x As Integer
arg = Evaluate({ @explode( Query_String ; "&") }, doc)
Dim tempdoc As notesdocument
Set tempdoc = session.currentdatabase.createdocument
If Attributes.Length > 0 Then
Dim attrname As String
For x = 1 To Attributes.Length
AttributeList(Lcase(attributes.GetName(x))) = Lcase(attributes.GetValue(attributes.GetName(x)))
Next
End If
’ To test for a parameter use IsElement ( listName ( stringExpr ) )
Exit Function
getAttributesError:
agentLog.LogAction( "getAttributesError: Got error " & Error$ & " on line " & Cstr(Erl) & "; Error" & Str(Err) & " :getAttributesError")
Resume Next
End Function