Android Tutorial XML Parsing with the XmlSax library

It is simpler to parse XML with Xml2Map class:

The XmlSax library provides an XML Sax parser.
This parser sequentially reads the stream and raises events at the beginning and end of each element.
The developer is responsible to do something useful with those events.

There are two events:
StartElement (Uri As String, Name As String, Attributes As Attributes)
EndElement (Uri As String, Name As String, Text As StringBuilder)
The StartElement is raised when an element begins. This event includes the element's attributes list.
EndElement is raised when an element ends. This event includes the element's text.

In this example we will parse the forum RSS feed. RSS is formatted using XML.
A simplified example of this RSS is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
        <title>Basic4ppc  / Basic4android - Android programming</title>
        <description>Basic4android - android programming and development</description>
            <title>Basic4ppc  / Basic4android - Android programming</title>
            <title>Phone library was updated - V1.10</title>
            <pubDate>Sun, 12 Dec 2010 09:27:38 GMT</pubDate>
            <guid isPermaLink="true"></guid>
        ...MORE ITEMS HERE
The first line is part of the XML protocol and is ignored.
On the second line the StartElement event will be raised with "Name = rss" and the attributes will include the "version" field.
The EndElement of the rss element will only be called on the last line: </rss>.

We will populate a list view with all items parsed from an offline file. When the user will press on an item we will open the browser with the relevant link.
Every item represents a forum thread.


For each item we are interested in two values. The title and the link.
The SaxParser object includes a handy list that holds the names of all the current parents elements.
This is useful as it will help us find the "correct" 'title' and 'link' elements. The correct elements are the ones under the 'item' element.

The parsing code in this case is pretty simple:
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)

End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If parser.Parents.IndexOf("item") > -1 Then
        If Name = "title" Then
            Title = Text.ToString
        Else If Name = "link" Then
            Link = Text.ToString
        End If
    End If
    If Name = "item" Then
        ListView1.AddSingleLine2(Title, Link) 'add the title as the text and the link as the value
    End If
End Sub
Title and Link are global variables.
We are only using EndElement events in this program.
First we check if we are inside an 'item' element. If this is the case we check the actual element name and save it if it is 'title' or 'link'.

If the current element is 'item' it means that we are done parsing an item.
So we add the data collected to the list view.

We are using ListView.AddSingleLine2. This method receives two values. The first is the item text and the second is the value that will return when the user will click on this item. In this case we are storing the link as the return value.

Later we will use it to open the browser:
Sub ListView1_ItemClick (Position As Int, Value As Object)
    StartActivity(PhoneIntents1.OpenBrowser(Value)) 'open the brower with the link
End Sub
The code that initiated the parsing is:
    Dim in As InputStream
    in = File.OpenInput(File.DirAssets, "rss.xml") 'This file was added with the file manager.
    parser.Parse(in, "Parser") '"Parser" is the events subs prefix.


    10 KB · Views: 6,141
Last edited:

Sebastiano Canfora

Licensed User
Good evening,
I have a problem with parsing an XML file.
Use the xmlsax library but the application after 5 sec gives me the message if close it or wait.
The xml file is a dataset with 31 columns and 3500 records the xml file download is immediate but no parsing.
you can parse asynchronously?
Sorry for my english


Sebastiano Canfora

Licensed User
In Release mode is ok but progress dialog indicator is blocked....
Another question can be created from a single insert xml file of the entire dataset? Now I create the insert syntax with the method parser_EndElement

Sebastiano Canfora

Licensed User
It wants to create an insert only but on sqlite what's the syntax?
"INSERTI NTO table1(column1, column2)



Licensed User
Longtime User
In this simple example based on this post i have problems when store a map into list variable, lost some data.
Can you help me please?

#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ListOfMaps As List
Dim dbmap As Map
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Dim text As String
text = "text"
For n=1 To 10
dbmap.Put("one", "'" & text & (n*10))
dbmap.Put("two", "'" & text & (n*10 + 1))
dbmap.Put("three", "'" & text & (n*10 + 2))
dbmap.Put("four", "'" & text & (n*10 + 3))


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub