Android Question Read & Parse XML

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I am a little stuck trying to parse a XML file.

I have checked out some of the examples (such as: http://www.b4x.com/android/forum/threads/xml-parsing-with-the-xmlsax-library.6866/#content) but still couldn't work out how to open a XML file and parse a item within it.

I have the following XML file

events.xml
B4X:
<?xml version="1.0"?>
<EVENTS>
  <EVENT>
    <Event_1001>event 1</Event_1001>
    <Extended_1001>1</Extended_1001>
    <Event_1002>event 2</Event_1002>
    <Extended_1002>3</Extended_1002>
    <Event_1003>event 3</Event_1003>
    <Extended_1003>1</Extended_1003>
    <Event_1004>event 4</Event_1004>
    <Extended_1004>2</Extended_1004>
  </EVENT>
</EVENTS>

I am trying to return 'event 3' (Event_1003) but can't work out how to select a single node (Event_1003) and get it's value (event 3)?

I am trying to return the value such as:

B4X:
Sub Process_Globals
 
  Dim xml_file As SaxParser
 
End Sub
 
 
Sub GetItem(event as string) as string
 
  xml_file.Initialize()
 
  Dim myFile As InputStream
 
  myFile = File.OpenInput(File.DirAssets, "events.xml")
 
  parser.Parse(myFile, "parser")
 
End Sub

I know I am doing it wrong, but can someone help me out?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
   Private parser As SaxParser
   Private events As Map
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     parser.Initialize
     events.Initialize
     parser.Parse(File.OpenInput(File.DirAssets, "1.xml"), "parser")
     Log(events)
   End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If parser.Parents.IndexOf("EVENT") > -1 Then 'we are inside the EVENT node
     events.Put(Name, Text.ToString)
   End If
End Sub
 
Upvote 0
Top