Subject: How to Parse an XML File for Input?
Hi Lemens,I got a start on how to do this from this forum and have taken it a bit further. The following code reads xml. The only weakness in the code is that you need to know how many levels or indentations that there are in the xml code. Fortunately this was not a problem for me. Hope this is of some use to you.
'This sub reads the xml file (4 levels) and then creates a notes document with the values read from the xml
Sub ReadXMLFile
Dim session As New NotesSession
Dim domParser As NotesDOMParser
Dim doc As NotesDocument
Dim db As NotesDatabase
Dim item As NotesItem
Dim strCurrentItemName As String
Dim strCurrentItemValue As String
Dim strFilename As String
Dim elementLevel_1 As NotesDOMElementNode
Dim elementLevel_2 As NotesDOMElementNode
Dim elementLevel_3 As Variant 'should be of type NotesDOMElementNode but causes error - even if you try to detect null
Dim elementLevel_4 As Variant 'should be of type NotesDOMElementNode but causes error - even if you try to detect null
Dim elementLevel_5 As Variant 'should be of type NotesDOMElementNode but causes error - even if you try to detect null
Dim elementValue As NotesDomNode
Dim docNode As NotesDOMDocumentNode
Dim intLevel_1_nodes As Integer
Dim intLevel_2_nodes As Integer
Dim intLevel_3_nodes As Integer
Dim xml_in As NotesStream
Dim intLevel_4_nodes As Integer
Dim index As Integer
Set db = session.CurrentDatabase
Dim result As Integer
result = Shell ("e:\Lotus\Domino\Data\MapToLocalView.bat",6)
Sleep( 10)
strFilename = "\\server\FaultReports\LocalViewReport_132917.2552007.1.xml"
’ strFilename = “c:\LocalViewFaults\LocalViewReport_4.xml”
Set xml_in = session.CreateStream
If Not xml_in.Open(strFilename) Then
Print "cannot open " & strFilename, , "XML file error"
Exit Sub
End If
Set domParser = session.CreateDomParser(xml_in)
domParser.Process
Set docNode = domParser.Document
'get the first element node - ReportInformation in above xml sample
Set elementLevel_1 = docNode.DocumentElement.FirstChild
'loop for all the fist level element nodes - (ReportInformation, ReportedBy, IncidentLocation, IncidentSpecificInformation)
While intLevel_1_nodes < docnode.DocumentElement.NumberOfChildNodes
'set the first level 2 element node (ReportID)
Set elementLevel_2 = elementLevel_1.FirstChild
'if it's an element node i.e. not data
If DOMNODETYPE_ELEMENT_NODE Then
strCurrentItemName = elementLevel_1.TagName
End If
'loop for all the second level element nodes
While intLevel_2_nodes < elementLevel_1.NumberOfChildNodes
If DOMNODETYPE_ELEMENT_NODE Then
strCurrentItemName = elementLevel_2.TagName
End If
'To determine if this element has data we need to check if the first child is null. If it is null then
'we still want to check the next element at this level because it may not be null
If Not elementLevel_2.FirstChild.isnull Then
'set the first level 3 element node - there won't always be one
Set elementLevel_3 = elementLevel_2.FirstChild
'loop for all the third level element nodes
While intLevel_3_nodes < elementLevel_2.NumberOfChildNodes
'not all level three elements will have children
If elementLevel_3.HasChildNodes Then
If DOMNODETYPE_ELEMENT_NODE Then
strCurrentItemName = elementLevel_3.TagName
End If
'To determine if this element has data we need to check if the first child is null. If it is null then
'we still want to check the next element at this level because it may not be null
If Not elementLevel_3.FirstChild.isnull Then
'set the first level 4 element node - there won't always be one
Set elementLevel_4 = elementLevel_3.FirstChild
'not all level four elements will have children
If elementLevel_4.HasChildNodes Then
If DOMNODETYPE_ELEMENT_NODE Then
strCurrentItemName = elementLevel_1.TagName
End If
'loop for all the fourth level element nodes
While intLevel_4_nodes < elementLevel_3.NumberOfChildNodes
If DOMNODETYPE_ELEMENT_NODE Then
strCurrentItemName = elementLevel_4.TagName
End If
'To determine if this element has data we need to check if the first child is null. If it is null then
'we still want to check the next element at this level because it may not be null
If Not elementLevel_4.FirstChild.isnull Then
'set the first level 5 element node - there won't always be one
Set elementLevel_5 = elementLevel_4.FirstChild
'get the data value of the level four node
strCurrentItemValue = elementLevel_4.Firstchild.NodeValue
'data node
If DOMNODETYPE_TEXT_NODE Then
If strCurrentItemValue <> "" Then
’ Msgbox strCurrentItemName & " " & strCurrentItemValue
UpdateFixedElementArray strCurrentItemName, strCurrentItemValue
End If
End If
End If
'If there is another sibling at this fourth level then move to it
If Not elementLevel_4.NextSibling.isnull Then
Set elementLevel_4 = elementLevel_4.NextSibling
End If
'increment the level 4 node count
intLevel_4_nodes = intLevel_4_nodes + 1
Wend 'end of fourth level
'all level 4 nodes have been handled for a section so reset the count
intLevel_4_nodes = 0
End If
'get the data value of the level three node
strCurrentItemValue = elementLevel_3.Firstchild.NodeValue
If DOMNODETYPE_TEXT_NODE Then
If strCurrentItemValue <> "" Then
’ Msgbox strCurrentItemName & " " & strCurrentItemValue
UpdateFixedElementArray strCurrentItemName, strCurrentItemValue
End If
End If
End If
End If
'If there is another sibling at this third level then move to it
If Not elementLevel_3.NextSibling.isnull Then
Set elementLevel_3 = elementLevel_3.NextSibling
End If
'increment the level 3 node count
intLevel_3_nodes = intLevel_3_nodes + 1
Wend 'end of third level
'all level 3 nodes have been handled for a section so reset the count
intLevel_3_nodes = 0
'get the data value of the level two node
strCurrentItemValue = elementLevel_2.Firstchild.NodeValue
If DOMNODETYPE_TEXT_NODE Then
If strCurrentItemValue <> "" Then
’ Msgbox strCurrentItemName & " " & strCurrentItemValue
UpdateFixedElementArray strCurrentItemName, strCurrentItemValue
End If
End If
End If
'If there is another sibling at this second level then move to it
If Not elementLevel_2.NextSibling.isnull Then
Set elementLevel_2 = elementLevel_2.NextSibling
End If
'increment the level 2 node count
intLevel_2_nodes = intLevel_2_nodes + 1
Wend 'end of second level
'all level 2 nodes have been handled for a section so reset the count
intLevel_2_nodes = 0
'If there is another sibling at this first level then move to it
If Not elementLevel_1.NextSibling.isnull Then
Set elementLevel_1 = elementLevel_1.NextSibling
End If
'increment the level 1 node count
intLevel_1_nodes = intLevel_1_nodes + 1
Wend 'end of first level
'The next section creates a notes document
Set doc = New NotesDocument(db)
'save all the fields that can contain data into the notes document
For index = 1 To MAX_ELEMENTS
Set item = doc.ReplaceItemValue( strFixedElementArray(index, 1), strFixedElementArray(index, 2) )
Next
doc.Form = "Local View"
Call doc.Save(True, False)
End Sub
If you need a sample copy of the xml I used then let me know.
Alex