How to Parse an XML File for Input?

I have posted on here before about reading XML files but not much luck, so don’t know if anyone can help. I have attached below an extract from my XML.

The XML file contains 10,000 applications with approx 20,000 attachments.

All I want to do is read the XML file and find each and every element and create a document in a Notes db. The latter part is the easy bit!

How do I loop through the entire file finding and processing each path element?

Any help/sample code much appreciated.

Thanks.

Credit Card Application

application01

/creditcard/application01.doc

Credit Card Application

application02

/creditcard/application02.doc

Personal Loan Application

loanapplication01

/creditcard/loanapplication01.doc

Credit Card Application

application01

/creditcard/application01.doc

Credit Card Application

application01

/creditcard/application01.doc

Subject: How to Parse an XML File for Input?

Subject: RE: How to Parse an XML File for Input?

You could also use an XSL file, instead of the DOMParser. I find it to be much easier.

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/c693c9a14f20f92c852571bd006a1eca?OpenDocument

http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/eec5b4bda550c9cc852572ce00785586?OpenDocument

Subject: How to Parse an XML File for Input?

You can also use the NoteSAXParser … which is a little bit easier to use in my opinion … you set up a few events to respond to calls from the SAXParser, and tell it to process.

I have an example on my blog that reads an XML file, and adds an attribute to it for unread marks on the web, it should show you how to parse through the XML File.

http://www.zetaone.com/jeremy/hodgebloge.nsf/d6plinks/JGHE-79AP3H

Jeremy

http://www.zetaone.com/jeremy/hodgebloge.nsf

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

Subject: How to Parse an XML File for Input?

Imo, if the input is fixed and is a one-shot deal then bypass XML code altogether.

If the file is laid out as in your post, i.e. separate lines, no extraneous white-space.

o Open the file for input

o Read each line.

o If the line starts with , then extract the stuff between and and use it.

Again, this is based on a lot of ifs and does not provide any future re-usability being a one-off.

Subject: How to Parse an XML File for Input?

Lemens,

If you only need the elements at the front-end of your application in a web browser, you can use JavaScript’s getElementsByTagName() method to get an array of elements…

var pathArray = yourXML.getElementsByTagName(“path”);

…and then set up a “for” loop to retrieve each pathArray value.

Ken