例: NotesSAXParser class

次の例は、XMLIN.XML ファイルを解析し、各イベントのメッセージボックスを表示します。エラー、致命的なエラー、警告は、SAXPARSER.TXT 出力ファイルに書き込まれます。

(Options)
%INCLUDE "lsconst.lss"

Sub Initialize
  
  Dim session As New NotesSession
  Dim saxParser As NotesSAXParser
  
  Dim xml_in As NotesStream
  filename$ = "c:¥dxl¥xmlin.xml"  ' open input file
  Set xml_in=session.CreateStream
  If Not xml_in.Open(filename$) Then
    Messagebox "Cannot open " & filename$,, "XML file error"
    Exit Sub
  End If
  If xml_in.Bytes = 0 Then
    Messagebox filename$ & " is empty",, "XML file error"
    Exit Sub
  End If
  
  Dim xml_out As NotesStream
  filename$ = "c:¥dxl¥saxparser.txt"  ' create output file
  Set xml_out=session.CreateStream
  If Not xml_out.Open(filename$) Then
    Messagebox "Cannot create " & filename$,, "TXT file error"
    Exit Sub
  End If
  xml_out.Truncate
  
  Set saxParser=session.CreateSAXParser(xml_in, xml_out)
  
  On Event SAX_Characters From saxParser Call SAXCharacters
  On Event SAX_EndDocument From saxParser Call SAXEndDocument
  On Event SAX_EndElement From saxParser Call SAXEndElement
  On Event SAX_Error From saxParser Call SAXError
  On Event SAX_FatalError From saxParser Call SAXFatalError
  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_StartDocument From saxParser Call SAXStartDocument
  On Event SAX_StartElement From saxParser Call SAXStartElement
  On Event SAX_UnparsedEntityDecl From saxParser Call SAXUnparsedEntityDecl
  On Event SAX_Warning From saxParser Call SAXWarning
  
  saxParser.Process  ' initiate parsing
  
End Sub

Sub SAXStartDocument (Source As Notessaxparser)
  Messagebox "Start reading Document", MB_ICONINFORMATION
End Sub

Sub SAXEndDocument (Source As Notessaxparser)
  Messagebox "End of Document", MB_ICONINFORMATION
End Sub

Sub SAXCharacters (Source As Notessaxparser, Byval Characters As String, _
Count As Long)
  Messagebox "Characters found", MB_ICONINFORMATION
  Source.Output (Characters)
End Sub

Sub SAXEndElement (Source As Notessaxparser, Byval ElementName As String)
  Messagebox "End of Element", MB_ICONINFORMATION
End Sub

Sub SAXError (Source As Notessaxparser, Exception As NotesSaxException )
  Messagebox "Error - "+Exception.Message, MB_ICONINFORMATION
  Source.Output ("Error - "+Exception.Message)
End Sub

Sub SAXFatalError (Source As Notessaxparser, Exception As NotesSaxException)
  Messagebox "FatalError - "+Exception.Message, MB_ICONINFORMATION
  Source.Output ("FatalError - "+Exception.Message)
End Sub

Sub SAXIgnorableWhitespace (Source As Notessaxparser,_
Byval characters As String, Count As Long)
  Messagebox "Ignorable Whitespace found", MB_ICONINFORMATION
End Sub

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

Sub SAXProcessingInstruction (Source As Notessaxparser,_
Byval target As String, Byval PIData As String)
  Messagebox "Processing Instruction found", MB_ICONINFORMATION
End Sub

Sub SAXStartElement (Source As Notessaxparser,_
Byval elementname As String, Attributes As NotesSaxAttributeList)
  Dim i As Integer
  Messagebox "Start reading Element name = "+elementname, MB_ICONINFORMATION
  If Attributes.Length > 0 Then
    Dim attrname As String
    For i = 1 To Attributes.Length
      attrname = Attributes.GetName(i)
      ' test GetValue and GetType args two ways _
      ' - by attribute name and by attribute index
      Messagebox "Attribute "+attrname+" = "_
      +Attributes.GetValue(attrname)+", type = "_
      +Attributes.GetType(attrname), MB_ICONINFORMATION
      Messagebox "Attribute "+attrname+" = "+Attributes.GetValue(i)+_
      ", type = "+Attributes.GetType(i), MB_ICONINFORMATION
      Source.Output("Attribute "+attrname+_
      " = "+Attributes.GetValue(attrname)+_
      ", type = "+Attributes.GetType(attrname))
    Next
  End If
End Sub

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

Sub SAXWarning (Source As Notessaxparser, Exception As NotesSaxException)
  Messagebox "Warning - "+Exception.Message, MB_ICONINFORMATION
  Source.Output("Warning - "+Exception.Message)
End Sub