Need to create Notes documents from XML text file

I've used delimited text files to create Notes documents many times, but before I write yet another text parsing script I'd like to see if I can use XML since the exported data file is in XML format this time.  Here is my situation, I have a text file exported from another application that I need to use to create and populate new documents in Notes.  The fields are all text values.  This application needs to run on the Notes for OS/X client, so using the Microsoft XML toolkit isn't an option.
Does anyone have a code sample to help get me started?
Thanks
Ed_CLPAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NARobertsCommented:
I know that notes 7 has alot more XML features built in.  What version are you on?
0
Ed_CLPAuthor Commented:
I'm using version 6.5.4.  This application needs to run on the user client (no version 7 available yet for Mac OS/X).
0
NARobertsCommented:
Here is a developerworks article on XML in 6.  I've never done it yet so I don't know that I can be much help to you, sorry.

http://www-128.ibm.com/developerworks/lotus/library/ls-LS_XML1/index.html

Also, a redbook.
http://www.redbooks.ibm.com/abstracts/sg246207.html
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Sjef BosmanGroupware ConsultantCommented:
Try first to export some documents from a Notes database to XML. There are some easy scripts in the Help database, look for the NotesDXLExporter method.
0
Ed_CLPAuthor Commented:
My LotusScript parser is looking better already!  I also found an article on Lotus Advisor from May 2001 that uses SAX and a Java agent to do what I'm trying to accomplish.  I'll hold the question open a bit longer before I use my original method.
0
Sjef BosmanGroupware ConsultantCommented:
Of course, there is the parallel NotesDXLImporter to get documents in...
0
Ed_CLPAuthor Commented:
It looks like NotesDXLImporter would work - can you find an example of using this to create populated documents in a Notes DB?  I can only find an example to create a new DB, not populate documents.
0
Sjef BosmanGroupware ConsultantCommented:
Ah, okay. The other day, I wrote some script to export documents from one db to import them into a second. The file was to be sent to some other party through mail.

Option Public
Option Explicit

Sub ExportDocs(dc As NotesDocumentCollection, filename As String)
    Dim ns As New NotesSession
    Dim db As NotesDatabase
    Dim stream As NotesStream

    Set db= ns.CurrentDatabase

    REM Open xml file named after current database
    Set stream= ns.CreateStream
    If Not stream.Open(filename) Then
        Messagebox "Cannot open " & filename,, "Error"
        Exit Sub
    End If
    Call stream.Truncate

    REM Export note collection as DXL
    Dim exporter As NotesDXLExporter
    Set exporter= ns.CreateDXLExporter(dc, stream)
    Call exporter.Process
End Sub

Sub ImportDocs(filename As String)
    Dim ns As New NotesSession
    Dim db As NotesDatabase
    Dim stream As NotesStream

    Set db= ns.CurrentDatabase

    REM Open xml file named after current database
    Set stream= ns.CreateStream
    If Not stream.Open(filename) Then
        Messagebox "Cannot open " & filename,, "Error"
        Exit Sub
    End If
    If stream.Bytes = 0 Then
            Messagebox "File did not exist or was empty",, filename
            Exit Sub
    End If

    REM Export note collection as DXL
    Dim importer As NotesDXLImporter
    Set importer= ns.CreateDXLImporter(stream, db)
    Call importer.Process
End Sub

Watch it though, it will import ANYTHING, even design documents!
0
p_parthaCommented:
YOu can try this download:
http://www.chc-3.com/downloads.htm

Partha
0
mshogrenCommented:
Hi, I am coming in late on this one but hopefully I can provide some help.  If all you are doing is creating documents and fields on notes documents from an XML document and you don't want to write a text parsing loop then try using the NotesDOMParser or NotesSAXParser to do the parsing for you.  I have done this recently and have provided two code examples below.  Notes that the extra code you write for the SAX parser results in faster processing for simple XML documents, but if you have a complex DOM structure in your XML documents it might be easier to use the DOM parser.

NotesDOMParser example:

Sub Initialize
      Dim startTime As Single
      startTime = Timer
      
      Dim ses As New NotesSession
      Dim db As NotesDatabase
      Dim doc As NotesDocument
      Dim parser As NotesDOMParser
      Dim xml As NotesStream
      Dim root As NotesDOMDocumentNode
      
      Set db = ses.CurrentDatabase
      
      'Open the XML file
      Set xml = ses.CreateStream
      If Not xml.Open(XML_FILE) Then
            Exit Sub
      End If
      
      'parse the XML into a DOM tree
      Set parser = ses.CreateDOMParser(xml)
      parser.Process
      Set root = parser.Document
      
      'Get a list of EMPLOYER elements
      Dim employers As NotesDOMNodeList
      Set employers = root.GetElementsByTagName("EMPLOYER")
      
      Dim el As NotesDOMNode
      Dim i As Long
      For i = 1 To employers.NumberOfEntries
            Set doc = db.CreateDocument
            'read all the child elements of the EMPLOYER element
            Set el = employers.GetItem(i).FirstChild
            Do While Not el.NextSibling.IsNull
                  'We are only checking XML elements not attributes or text nodes
                  If el.NodeType = 1 Then
                        Dim fieldName As String
                        Dim fieldValue As String
                        
                        'transform the element name to the correct field name (probably a select statement)
                        fieldName = el.LocalName
                        'transform the element value to the correct field value
                        'Note: this value is only correct based on the assumption that
                        'the sub-elements of EMPLOYER contain no sub-elements themselves
                        fieldValue = el.FirstChild.NodeValue
                        'assign the field the value
                        doc.ReplaceItemValue fieldName, fieldValue
                        
                  End If
                  
                  Set el = el.NextSibling
            Loop
            'compute any other required fields here
            'check whether we want to delete an old document and save this one
            doc.Save True, True
      Next
      
      Print "Time taken (DOM): " + Cstr(Timer - startTime)
End Sub


NotesSAXParser Example:

Class CORRSProcessor
      Private db As NotesDatabase
      Private doc As NotesDocument
      Private parser As NotesSAXParser
      Private currentvalue As String
      
      Public Sub New(db As NotesDatabase, xml As NotesStream)
            Set Me.db = db
            Set parser = db.Parent.CreateSAXParser(xml)
            
            On Event SAX_Characters From parser Call SAXCharacters
            On Event SAX_EndDocument From parser Call SAXEndDocument
            On Event SAX_EndElement From parser Call SAXEndElement
            On Event SAX_Error From parser Call SAXError
            On Event SAX_FatalError From parser Call SAXFatalError
            On Event SAX_IgnorableWhitespace From parser Call SAXIgnorableWhitespace
            On Event SAX_NotationDecl From parser Call SAXNotationDecl
            On Event SAX_ProcessingInstruction From parser Call SAXProcessingInstruction
            On Event SAX_StartDocument From parser Call SAXStartDocument
            On Event SAX_StartElement From parser Call SAXStartElement
            On Event SAX_UnparsedEntityDecl From parser Call SAXUnparsedEntityDecl
            On Event SAX_Warning From parser Call SAXWarning
      End Sub
      
      Public Sub Process
            parser.Process
      End Sub
            
      Private Sub SAXStartDocument (Source As Notessaxparser)
            
      End Sub
      
      Private Sub SAXEndDocument (Source As Notessaxparser)
            
      End Sub
      
      Private Sub SAXStartElement (Source As Notessaxparser, Byval elementname As String, Attributes As NotesSaxAttributeList)
            If elementname = "WITExtract" Then
                  'Ignore the document root
            Elseif elementname = "EMPLOYER" Then
                  'Create a new document for each employer
                  Set doc = Me.db.CreateDocument
            Else
                  Me.currentValue = ""
            End If      
      End Sub
      
      Private Sub SAXCharacters (Source As Notessaxparser, Byval Characters As String, Count As Long)
            Me.currentValue = Me.currentValue + Characters
      End Sub
      
      Private Sub SAXEndElement (Source As Notessaxparser, Byval ElementName As String)
            On Error Goto errorh
            
            Dim fieldName As String
            Dim fieldValue As String
            
            If elementname = "WITExtract" Then
                  'Ignore the document root
            Elseif elementname = "EMPLOYER" Then
                  'compute any other required fields here
                  'check whether we want to delete an old document and save this one
                  doc.Save True, True
            Else
                  'transform the element name to the correct field name (probably a select statement)
                  fieldName = elementname
                  
                  'transform the element value to the correct field value
                  fieldValue = Me.currentValue
                                    
                  'assign the field the value
                  doc.ReplaceItemValue fieldName, fieldValue
            End If
            Exit Sub
            
errorh:
            Exit Sub
      End Sub
      
      Private Sub SAXError (Source As Notessaxparser, Exception As NotesSaxException )
            
      End Sub
      
      Private Sub SAXFatalError (Source As Notessaxparser, Exception As NotesSaxException)
            
      End Sub
      
      Private Sub SAXIgnorableWhitespace (Source As Notessaxparser,Byval characters As String, Count As Long)
            
      End Sub
      
      Private Sub SAXNotationDecl (Source As Notessaxparser,Byval NotationName As String, Byval publicid As String, Byval systemid As String)
            
      End Sub
      
      Private Sub SAXProcessingInstruction (Source As Notessaxparser, Byval target As String, Byval PIData As String)
            
      End Sub
      
      Private Sub SAXUnParsedEntityDecl (Source As Notessaxparser, Byval Entityname As String, Byval publicid As String, Byval systemid As String, Byval notationname As String)
            
      End Sub
      
      Private Sub SAXWarning (Source As Notessaxparser, Exception As NotesSaxException)
            
      End Sub
      
End Class

Sub Initialize
      Dim startTime As Single
      startTime = Timer
      
      Dim ses As New NotesSession
      Dim db As NotesDatabase
      Dim xml As NotesStream
      Dim CP As CORRSProcessor
      
      Set db = ses.CurrentDatabase
      
      'Open the XML file
      Set xml = ses.CreateStream
      If Not xml.Open(XML_FILE) Then
            Exit Sub
      End If
      
      Set CP = New CORRSProcessor(db, xml)
      CP.Process
      
      Print "Time taken (SAX): " + Cstr(Timer - startTime)
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.