### Parse XML file into a TreeView

This an example how to parse data, using the SaxParser, from an XML file into a TreeView.
The example TreeView has 3 Levels:
Level 0 = Root, Level 1 = Category, Level 2 = Title
A Category has to be unique and can have multiple Titles.
XML File Structure
The XML entry nodes are parsed. Each entry has a node Category and Title.
As mentioned, parsing into the TreeView results in TreeItem Category (Level 1) with
multiple TreeItems title (Level 2).
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<entry>
<category>Category 1</category>
<title>Title 1</title>
</entry>
<entry>
<category>Category 2</category>
<title>Title 2</title>
</entry>
<entry>
<category>Category 1</category>
<title>Title 3</title>
</entry>
<entry>
</root>
Define Globals
`Sub Process_Globals
Private TreeView1 As TreeView
Private FXMLParser As SaxParser
Private FXMLcategory, FXMLtitle As String
...
End Sub`
Init the Parser
This can be done in Sub AppStart or in another Sub, but important to set prior parsing
FXMLParser.Initialize
Define Parser Routine
'Parse (=Load) the data from the xml file into the treeview
`Sub parseXMLDataToTreeView
' Define the xml file to parse. The testfile is located in the app folder (same as the jar file)
' The XM file structure is as previous outlined
Dim xmlFile As String = "importxmlexample.xml"
If File.Exists(File.DirApp, xmlFile) = False Then
Log("Error: XML file to parse not found.")
Return
End If
' Clear all nodes from the treeview
TreeView1.Root.Children.Clear
'parse the xml file
Dim In As InputStream
Try
In = File.OpenInput(File.DirApp, xmlFile)
FXMLParser.Parse(In, "Parser")
In.Close
Log("Information: Imported Titles from XML File: " & xmlFile)
Catch
Log("Error: XML Import: " & LastException.Message)
End Try
End Sub`
'XML parser startelement not used
`Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
End Sub`
'Parse the xml node.
'The EndElement event is used, as check if inside an 'entry' element.
'If the case then check the actual element name and assign to the textfields.
'If the current element is 'entry' then done with parsing an entry.
'The entry is inserted into the treeview.
'The field category is check if already in the treeview as a treeitem. if not will be created.
`Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
' Check if for an entry child nodes are found. If so, then assign the xml nodes to the global vars
If FXMLParser.Parents.IndexOf("entry") > -1 Then
' Level 1 TreeItem = Category
If Name = "category" Then FXMLcategory = Text.ToString
' Level 2 TreeItem = Title (Parent is Category)
If Name = "title" Then FXMLtitle = Text.ToString
' Add more Level 2 TreeItem
End If
' End Element is found.
' Add a new category and children or if category already exists add children only
If Name = "entry" Then
' Create a new category treeitem and initialize
Dim catti As TreeItem
catti.Initialize("catti", FXMLcategory)
' Search for category treeitem first befor adding a new category
Dim idx As Int = getTreeItemIndex(TreeView1, FXMLcategory)
' category treeitem already in the treeview
' Get the child and assign to the treeitem used to add the children
If idx <> -1 Then
catti = TreeView1.Root.Children.Get(idx)
Else
' Add a new category to the root node and expand
TreeView1.Root.Children.Add(catti)
catti.Expanded = True
End If
' Add the title to the category treeitem as a child
Dim titleti As TreeItem
titleti.Initialize("catti", FXMLtitle)
catti.Children.add(titleti)
' Add more ...
End If
End Sub`
'Search for a treeitem nodetext and return the index from the children list
'The search will only check at level 1 nodes (=the children of the root node)
'Parameter Treeview
'Parameter Findtext as String
'Returns -1 if nodetext not found else index of the node in the children list
`Sub getTreeItemIndex(tv As TreeView, findtext As String) As Int
For i = 0 To tv.Root.Children.Size - 1
Dim ti As TreeItem = tv.Root.Children.Get(i)
If findtext.ToUpperCase = ti.Text.ToUpperCase Then Return i
Next
Return -1
End Sub`
Example calling parser routine as a Button event
' Private buttonParseXML As Button
`Sub buttonParseXML_Action
parseXMLDataToTreeView
End Sub`
Resulting in:
+Category 1
+-Title 1
+-Title 3
+Category 2
+-Title 2

Changed: , Category: XML, File: T6C285M2143.htm, ID: 2143