B4J Question Encountering difficulties with Xml2Map parser with small files

coley

Member
Licensed User
Longtime User
Hi Guys, I winder if you can help?

I'm using the Xml2Map class to extract data from an XML response from a HTTP server, it appeared to be working just great until I started restricting the number of results down to zero.
The class works fine down to two results return but one result returned and zero results returned cause the following error.

B4X:
Waiting for debugger to connect...
Program started.
Parsing File
[Fatal Error] :1:1: Content is not allowed in prolog.
Error occurred on line: 24 (Xml2Map)
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at anywheresoftware.b4a.objects.SaxParser.parse(SaxParser.java:80)
    at anywheresoftware.b4a.objects.SaxParser.Parse(SaxParser.java:73)
    at b4j.example.xml2map._parse2(xml2map.java:254)
    at b4j.example.xml2map._parse(xml2map.java:89)
    at b4j.example.main._parsexml(main.java:128)
    at b4j.example.main._appstart(main.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)

I've tried looking through the class but can't figure out whats happening.

I've grabbed the results responses from the web service, put them into files then rewrote the code to accept the file as the input stream, same results for both online and file based XML.

Here is my code.

B4X:
Sub ParseXML
    Dim xm As Xml2Map
    xm.Initialize
    Log("Parsing File")
    Dim PCR As Map = xm.Parse(File.ReadString(File.DirAssets, "NoResults.xml"))
    Dim rsp As Map = PCR.Get("rsp")
    Dim PeopleCount As Map = rsp.Get("PeopleCount")
    Dim items As List = PeopleCount.Get("item")
    For Each entry As Map In items
        Dim PeopleCountRecords As Map = entry.Get("PeopleCountRecord")
        Dim Cardholder As String = PeopleCountRecords.Get("Cardholder")
        Dim LastAccess As String = PeopleCountRecords.Get("LastAccess")
        Dim Status As String = PeopleCountRecords.Get("Status")
        Log (Cardholder & " " & LastAccess & " " & Status)
    Next
End Sub

Here are the XML files and the Xml2Map class.

Any help will be much appreciated.
 

Attachments

  • ManyResults.xml
    2.1 KB · Views: 234
  • OneResult.xml
    246 bytes · Views: 224
  • TwoResults.xml
    436 bytes · Views: 227
  • Xml2Map.bas
    2.6 KB · Views: 237
  • NoResults.xml
    56 bytes · Views: 239

coley

Member
Licensed User
Longtime User
The xml is not valid. There is an extra question mark at the beginning.

Apologies Erel, that isn't supposed to be in the file, it must have been a stry character when I made the file.
I've uploaded the correct one now. This code fails on one or no results but works perfectly with two or more results..
 
Upvote 0
Top