Parse XML using InputStream

susu

Well-Known Member
Licensed User
Longtime User
I used to save XML file to device then parse it using XMLSax library. But now I want to parse XML file directly with this code:

B4X:
Sub http_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim result As InputStream
result = Response.GetInputStream
parser.Parse(result, "Parser")
End Sub


Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If parser.Parents.IndexOf("Item") > -1 Then
      If Name = "Time" Then
         Time.Text = Text.ToString
      Else If Name = "Desc" Then
         Desc.Text = Text.ToString
            ListView1.AddTwoLines(Time, Desc)
      End If
   End If
End Sub

The code compiled without error but it display like the screenshot below, I just wonder what's wrong here?


attachment.php
 

Attachments

  • error.jpg
    error.jpg
    24.6 KB · Views: 1,314

susu

Well-Known Member
Licensed User
Longtime User
Oooppss, my bad :D
Thanks Agraham.
 
Upvote 0

mrB

New Member
Licensed User
Longtime User
As far as I understand this thread it should be possible to parse an xml stream without saving the xml to a file, but unfortunately I'm not able to make it work. Anyone spotting what I've missed (?) :

B4X:
'Activity module

Sub Activity_Create(FirstTime As Boolean)

   Dim req As HttpRequest
   Dim hc As HttpClient

   hc.Initialize("hc")
   req.InitializeGet("http://yr.no/sted/Norge/Oslo/Oslo/Oslo/varsel.xml")

   hc.Execute(req,1)
   
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)

   Dim Parser As SaxParser
   Dim Result As InputStream

   Result = Response.GetInputStream

   Log(Result)
   Dim buffer(20000) As Byte
   Log(Result.ReadBytes(buffer,0,buffer.Length))   

   '   parse the xml file

   Parser.Parse(Result, "Parser")

   Result.Close

End Sub

Sub Parser_EndElement (Uri As String, Name As String, Attributes As Attributes)

   '   do stuff

End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)

   '   do stuff

End Sub

Sub hc_ResponseError(Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)

   '   catch error reason
   
End Sub
I receive "an error has occurred in sub: main_hc_responsesuccess (java line:293) java.lang.NullPointerException"
 
Upvote 0
Top