SNTT – XML importer class

Yes, I know it is Tuesday, but I was too busy last week for the SNTT entry…

 

At my workplace, we are in the process of purchasing and implementing a new billing system. It relies heavily on XML files, stored in the file system, for inport/export. The idea is that we export XML from our policy system to be processed by this software. The results are returned as XML files, and we are currently looking at how we can consume them and present them to the accounting department.

We are mainly looking at tools like Crystal Reports 2008.

Just for fun, I decided to write an XML importer in Lotusscript. The XML is very simple so I did not have to do any advanced parsing.
I decided to write it as a class. The class just have a few methods. One is of course New(), the only thing it does is clear the xmldata string and also clear the list of XML tags from memory.

LoadXMLData() open the specified XML file and read the text into the xmldata string, adding a linebreak after each line.

XML2List take the xmlstring, parse it and build a list of all the tags and values. Because the XML is so simple, with no multiple values or deeply nested data, I can very easily parse it using normal string operations in a Do-While loop.

The final method is called CreateNotesDocument(), it creates a new Notes document in the specified database, using the specified form. To process the sample XML file with 66 XMl entries took 0.148 seconds (according to TeamStudio Profiler).

Here is the code:

Class XMLClass
Private xmldata As String
Private xml List As String

Public Sub New()
  xmldata=""
  Erase xml
End Sub

Public Sub LoadXMLData(filename As String)
  Dim xmlline As String
  Open filename For Input As #1
  xmldata = ""
  While Not Eof(1)
   Line Input #1, xmlline
   xmldata = xmldata & xmlline & Chr$(13)
  Wend
  Close #1
End Sub

Public Sub XML2List()
  Dim tag As String
  Dim value As String
  Dim startpos As Long
  Dim endpos As Long
  Dim nextpos As Long
  Dim endtagpos As Long
 
  endpos = 1
  Do 
   startpos = Instr(endpos,xmldata,"<")
   If startpos > 0 Then
    endpos = Instr(startpos,xmldata,">")
   End If
   If endpos > 0 Then
    tag = Mid$(xmldata,startpos+1,endposstartpos1)
    If Instr(tag,"/")=1 Then
     startpos = Instr(endpos+1,xmldata,"<")
    Else
     nextpos = Instr(endpos,xmldata,"<")
     endtagpos = Instr(endpos,xmldata,"</" & tag & ">")
     If endtagpos = ne
xtpos Then
      value = Mid$(xmldata,endpos+1,nextposendpos1)
      If Iselement(xml(tag)) = False Then
       xml(tag) = value
      End If
      endpos = Instr(nextpos,xmldata,">")
     End If
    End If   
   End If
  Loop While startpos > 0
End Sub

Public Sub CreateNotesDocument(db As NotesDatabase, form As String)
  Dim doc As NotesDocument
  Set doc = New NotesDocument(db)
  doc.Form = form
  Forall x In xml
   Call doc.ReplaceItemValue(Listtag(x),x)
  End Forall
  Call doc.Save(True,True)
End Sub
 
End Class

Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim noic As XMLclass

Set db = session.CurrentDatabase
Set noic = New XMLclass()
Call noic.LoadXMLData("c:XSLTNOIC-1450-01 0-2007Nov09.xml")
Call noic.XML2List()
Call noic.CreateNotesDocument(db,"NOIC")
End Sub

 

This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.

 

Leave a Reply