Android Tutorial XML Parsing with the XmlSax library

airblaster

Active Member
Licensed User
Longtime User
If you are parsing different XML files, can you reuse the parser object? Or is it safer to create a parser object for each XML file?
 

socialnetis

Active Member
Licensed User
Longtime User
If I have something like this:

B4X:
Sub HandleDownloadPage (Job As HttpJob)
   XmlParser.Initialize
   XmlParser.Parse(Job.GetInputStream, "XmlParser")
    DoSomething()            'Use the parsed data       
End Sub

Does the DoSomething() function will be executed after ALL the Input is parsed? Or the Parser runs in another thread?
 

netchicken

Active Member
Licensed User
Longtime User
For the life of my I can't parse this xml. I know it uses Attributes, as all the data is within the tags however there are two main parts, the <platform> and the <Position>.

Might they have to be processed separately?

Does anyone have any ideas?

I have been trying to get the data out with below in the Parser_StartElement
PlatformTag = Attributes.GetValue2("", "PlatformTag")




B4X:
 <Platform PlatformTag="1" Name="City Exchange (A)">
    <Position Lat="-4.353379800000000e+001" Long="1.726375730000000e+002" />
  </Platform>
  <Platform PlatformTag="2" PlatformNo="37323" Name="City Exchange (B)" RoadName="Lichfield St">
    <Position Lat="-4.353411900000000e+001" Long="1.726373400000000e+002" />
  </Platform>
  <Platform PlatformTag="3" PlatformNo="37334" Name="City Exchange (C)" RoadName="Lichfield St">
    <Position Lat="-4.353411900000000e+001" Long="1.726371900000000e+002" />
  </Platform>
  <Platform PlatformTag="4" PlatformNo="39911" Name="City Exchange (D1)" BearingToRoad="2.7112744e+002" RoadName="Colombo St">
    <Position Lat="-4.353377900000000e+001" Long="1.726366870000000e+002" />
  </Platform>
  <Platform PlatformTag="5" PlatformNo="39924" Name="City Exchange (E2)" BearingToRoad="9.1127449e+001" RoadName="Colombo St">
    <Position Lat="-4.353318461000000e+001" Long="1.726365578000000e+002" />
  </Platform>
 
Last edited:

netchicken

Active Member
Licensed User
Longtime User
OK Fixed it ... I think (I will post the fixed version when its done)
The big issue is that it is case sensitive, which got me.
Otherwise all working, maybe someone could use this if they find a similar structure.

Here is what I have so far ....

First lines of the datafile....

B4X:
<?xml version="1.0"?>
<JPPlatforms xmlns="urn:connexionz-co-nz:jp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:connexionz-co-nz:jp JourneyPlanner.xsd">
  <Content Expires="2011-05-15T03:32:00" />
  <Platform PlatformTag="1" Name="City Exchange (A)">
    <Position Lat="-4.353379800000000e+001" Long="1.726375730000000e+002" />
  </Platform>
  <Platform PlatformTag="2" PlatformNo="37323" Name="City Exchange (B)" RoadName="Lichfield St">
    <Position Lat="-4.353411900000000e+001" Long="1.726373400000000e+002" />
  </Platform>
  <Platform PlatformTag="3" PlatformNo="37334" Name="City Exchange (C)" RoadName="Lichfield St">
    <Position Lat="-4.353411900000000e+001" Long="1.726371900000000e+002" />
  </Platform>
  <Platform PlatformTag="4" PlatformNo="39911" Name="City Exchange (D1)" BearingToRoad="2.7112744e+002" RoadName="Colombo St">
    <Position Lat="-4.353377900000000e+001" Long="1.726366870000000e+002" />
  </Platform>
  <Platform PlatformTag="5" PlatformNo="39924" Name="City Exchange (E2)" BearingToRoad="9.1127449e+001" RoadName="Colombo St">
    <Position Lat="-4.353318461000000e+001" Long="1.726365578000000e+002" />
  </Platform>

Under Globals
B4X:
Type BusStop(PlatformName As String, PlatformTag As Int, PlatformNo As Int, RoadName As String, Lat As Long, Lon As Long)
   
Dim ListOfBusStops As List
Dim ThisBusStop As BusStop



Under Start Element
B4X:
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)

If Name = "Platform" Then
     
        ThisBusStop.PlatformTag = Attributes.GetValue2("", "PlatformTag")
        ThisBusStop.PlatformName  = Attributes.GetValue2("", "name")
        ThisBusStop.PlatformNo = Attributes.GetValue2("", "PlatformNo")
         ThisBusStop.RoadName = Attributes.GetValue2("", "RoadName")
  
End If

If Name = "Position" Then
      ThisBusStop.Lat = Attributes.GetValue2("", "Lat")
      ThisBusStop.Lon = Attributes.GetValue2("", "Long")
End If 


ListOfBusStops.Add(ThisBusStop)
 

Log("name " & ThisBusStop.PlatformName &" " & " Lat " & ThisBusStop.Lat &" " & " number " & ThisBusStop.PlatformNo)
 
Last edited:

Fabrice La

Active Member
Licensed User
Longtime User
Hi

I have this xml file.

1) I have to extract th id of each item --> ok

2) After choosing in list the id I need to extract the sample lines only in this id ?

any help
 

mterveen

Member
Licensed User
Longtime User
sax parser in code module

(erel - feel free to delete double post in general forum area on this).

does the xml sax parser work in a code module? if so can someone get the xml example from post 1 to work as a module.

i have no trouble getting my program to work in an activity with the xmlsax parser but moving it to the code module does not seem to trigger the start and end events.
 

shawny

Member
Licensed User
Longtime User
Having some issues getting this to work. I was able to use the HttpJob to download my XML file, used w3schools to validate file. Wether I use an inputstream and parse or textreader and parse2 I get the same issue. NullReference. Can't for the life of me figure this out, need some help.

LogCat connected to: B4A-Bridge: samsung SAMSUNG-SGH-I537-356554050382940
--------- beginning of /dev/log/main
running waiting messages (3)
** Activity (main) Resume **
** Service (service1) Destroy **
** Service (service1) Create **
** Service (service1) Start **
Connected to B4A-Bridge (Wifi)
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (listpendingshipmentscrmodule) Create, isFirst = true **
** Activity (listpendingshipmentscrmodule) Resume **
Error occurred on line: 52 (listpendingshipmentscrmodule)
java.lang.NullPointerException
at anywheresoftware.b4a.objects.SaxParser.parse(SaxParser.java:78)
at anywheresoftware.b4a.objects.SaxParser.Parse2(SaxParser.java:88)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:158)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:64)
at android.view.View.performClick(View.java:4354)
at android.view.View$PerformClick.run(View.java:17961)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
 

shawny

Member
Licensed User
Longtime User
Initializing the object worked. But now it's telling me the same after all items have been looped thru. Thanks NJDude
 

shawny

Member
Licensed User
Longtime User
Got it working now Had to move some declarations around. Gotta say, pretty impressed with the IDE as well as the community on this. Recently purchased it after 2 days of usage, got it integrating with my XML API I've been using for years in PHP. Have been able to bridge the gap now to Android Devices. Thanks b4a.
 

GMan

Well-Known Member
Licensed User
Longtime User
Cool Lib - works from the scratch
 

jcredk

Member
Licensed User
Longtime User
Hi all,
This library is great ... I am using it inside a kind of RSS Reader/Concentrator I am doing.

Despite I read all pages of this post I still have issues with encoding on some flows only.

I reuse the sample at the beginning of the post so you can see the behavior I have ...
Basically the application downloads the RSS feed of a french newspaper (text will contain french accentuated letters éèà ...) that are not properly handled.

You will see in the attachement below the test application highly inspired from top post sample ... Then accentuated letters are replaced with question mark inside a diamond (cf. ScreenShot) ...!

I tried many things with an intermediate stream to handle encoding but without success ...
... Any help from those that already solved this issue will be greatly appreciated !

Thanks,
 

Attachments

  • XmlSaxTest.zip
    1.1 KB · Views: 422
Cookies are required to use this site. You must accept them to continue using the site. Learn more…