Android Tutorial XML Parsing with the XmlSax library

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

I'm reading results from a hosted Json file and want to parse it. Below is the code I use but it does not work. xml is the string which contains the xml. I'm getting a nullpointer exception.

Dim In As InputStream
Dim data() As Byte = xml.GetBytes("UTF8")
In.InitializeFromBytesArray(data, 0, data.Length)
xmlParser.Parse(In, "parseXML")
 

paolofi

Member
Licensed User
Longtime User
Hi, I have a problem parsing an XML stream from my server.
If I parse the stream from URL the Text StringBuilder parameters of Sub Parser_EndElement contains already all the full stream character at the first cycle, if I copy and paste the XML stream in a text file and read it with File.OpenInput, the parser works well.
I use HttpUtils2 to connecting, I verified the XML stream and I believe is good.

Can you have any suggestions?

Thanks in advance.

Paolo


B4X:
Sub JobDone (job As HttpJob)

Dim In As InputStream

If job.Success = True Then
        'this doesn't works well
        In = job.GetInputStream
        parserCustomer.Parse(In, "Parser")
     
        'this works!
        In = File.OpenInput(File.DirAssets, "testxml.xml")

        In.Close    
End If

job.Release

End Sub

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)

Dim Result
Dim FieldsList

'Text contains the full stream no parsed.

If parserCustomer.Parents.IndexOf("string") > -1 Then
    If Name = "Result" Then
        Result = Text.ToString
    Else If Name = "FieldsList" Then
        FieldsList = Text.ToString
    End If
End If

End Sub

The testxml.xml file (captured from server stream)
B4X:
<string xmlns="http://MyDomain.com/">
<Result>OK|</Result><FieldsList>|counter|id|</FieldsList><Record>|24749|1204|</Record><Record>|40852|8136|</Record><Record>|41192|2748|</Record><Record>|41194|4516|</Record><Record>|41195|7772|</Record><Record>|41196|8915|</Record><Record>|41197|4343|</Record><Record>|41198|9999|</Record><Record>|41199|9999|</Record><Record>|41200|815|</Record><Record>|41201|9154|</Record>
</string>
 

paolofi

Member
Licensed User
Longtime User
What is the output of Log(Job.GetString)?

I used this:
B4X:
Sub JobDone (job As HttpJob)

Log("PROC:JobDone")

If job.Success = True Then
    Log("Job.GetString = " & job.GetString)
    Log("job.GetString End") 
End If

job.Release

End Sub

and the result was:
B4X:
PROC:JobDone
PROC:JobDone
Job.GetString = <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://MyDomain.com/">&lt;Result&gt;OK|&lt;/Result&gt;&lt;FieldsList&gt;|counter|id|&lt;/FieldsList&gt;&lt;Record&gt;|24749|1204|&lt;/Record&gt;&lt;Record&gt;|40852|8136|&lt;/Record&gt;&lt;Record&gt;|41192|2748|&lt;/Record&gt;&lt;Record&gt;|41194|4516|&lt;/Record&gt;&lt;Record&gt;|41195|7772|&lt;/Record&gt;&lt;Record&gt;|41196|8915|&lt;/Record&gt;&lt;Record&gt;|41197|4343|&lt;/Record&gt;</string>
Job.GetString = <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://MyDomain.com/">&lt;Result&gt;OK|&lt;/Result&gt;&lt;FieldsList&gt;|counter|id|&lt;/FieldsList&gt;&lt;Record&gt;|24749|1204|&lt;/Record&gt;&lt;Record&gt;|40852|8136|&lt;/Record&gt;&lt;Record&gt;|41192|2748|&lt;/Record&gt;&lt;Record&gt;|41194|4516|&lt;/Record&gt;&lt;Record&gt;|41195|7772|&lt;/Record&gt;&lt;Record&gt;|41196|8915|&lt;/Record&gt;&lt;Record&gt;|41197|4343|&lt;/Record&gt;</string>
Job.GetString End
Job.GetString End

The LOG repeats twice despite debugging i see passing on it only 1 time, I don't know if this is right.

The code pass from here when it call .Getstring:
B4X:
'Returns the response as a string encoded with UTF8.
Public Sub GetString As String
    Return GetString2("UTF8")
End Sub

'Returns the response as a string.
Public Sub GetString2(Encoding As String) As String
    Dim tr As TextReader
    tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)
    Dim res As String
    res = tr.ReadAll
    tr.Close
    Return res
End Sub

This can help?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

paolofi

Member
Licensed User
Longtime User

Thanks for your reply,
now i'm investigating about the string escaping, meanwhile this solve my problem:

B4X:
Dim s As String
Dim In As InputStream

If job.Success = True Then
        s=job.GetString
        s=s.Replace("&lt;","<")
        s=s.Replace("&gt;",">")
     
        In=StringToInputStream(s)
            
        parserCustomer.Parse(In, "Parser")
End If

Sub StringToInputStream (s As String) As InputStream
   Dim In As InputStream
   Dim data() As Byte = s.GetBytes("UTF8")
   In.InitializeFromBytesArray(data, 0, data.Length)
   Return In
End Sub

Many thanks Erel.
 

BedDweller

Member
Licensed User
Longtime User
Hello, not really a B4A question, but its concerning this lib... I'm currently making two programs which both read a XML file, I have used this lib in B4A and it works like a charm, however the other program is made in Visual Studio's and I was wondering if there is a lib for VS that works like this one (I've done alot of searching, but most are ether Dom or more complex) Thanks
 

holdemadvantage

Active Member
Licensed User
Longtime User
Hi, maybe this is a stupid question...

I use this lib to parse an online xml, all ok but i cannot understand how to check if all elements of my xml are parsed, is there a way to do it?

I explain my problem
During parsing i fill a list of xml elements then at the end of parsing i have to perform other action if this list is fill of all xml elements, how can i check if parsing is finished?
 

almontgreen

Active Member
Licensed User
Longtime User
I am just starting with B4J and trying to parse xml with this line in the xml:

<media:image url="http://www.6a3d.com/folder/images/gallery/Untitled-1.jpg" type="image/jpeg" height="337" width="450"/>

I only want the link. I don't want anything else like height etc. When I use the code:

B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder, Attributes As Attributes)
    If parser.Parents.IndexOf("item") > -1 Then
         If Name = "media:image" Then
             Log(Text.ToString)
         end if
'more stuff...
It is blank. I don't understand how to use Attributes.GetValue Could you please provide an example? I'm very confused. Thanks!!!
 

almontgreen

Active Member
Licensed User
Longtime User
There is no such event as the one you wrote.
The Attributes parameter is only available in Parser_StartElement.
Thanks. Tried both Parser_StartElement and Parser_EndElement and even with the right parameters, so far, I can't figure out how to parse out just the url.
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
i can find end of parse sax.
i check item name in end_element with "channer" expersion and i know than it is end if parse
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…