Help needed with retrieving XML Values.

Peter Simpson

Expert
Licensed User
Longtime User
Hello everybody,
I've been up all night trying to figure out how to parse just one day of data from the following link, lets say Wed(for Wednesday).

http://www.google.com/ig/api?weather=birmingham,uk

I used as much information from post47454 as possible and I even looked at post39737.

I just can't seem to get the correct data from the XML feed.

I can get the xml using HttpUtils and save it onto the device just fine, I just need to filter the following information. I can also read the day, not not the following information for that day.

<day_of_week data="Wed"/>
<low data="52"/>
<high data="64"/>
<icon data="/ig/images/weather/chance_of_storm.gif"/>
<condition data="Chance of Storm"/>

This is correct:
Sub xml_StartElement(Uri As String, Name As String, Attributes As Attributes)
If Name = "day_of_week" AND parser.Parents.IndexOf("forecast_conditions") > -1 Then
If Attributes.GetValue2("", "data") = "Wed" Then
Log("Yes day correct")
currentName = Attributes.GetValue2("", "data")
End If
End If
End Sub

But this is not:
Sub xml_EndElement(Uri As String, Name As String, Text As StringBuilder)
Dim Low, High, Icon, Condition As String

If Name = "day_of_week" AND parser.Parents.IndexOf("forecast_conditions") > -1 Then
Select currentName
Case "low"
Log("Low: " & Text.ToString)
Case "high"
Log("High: " & Text.ToString)
Case "icon"
Log("Icon: " & Text.ToString)
Case "condition"
Log("Condition: " & Text.ToString)
End Select
End If
End Sub

I've been looking all over the forums for hours just for the fix and I've now got an headache :confused:

Any help or code rewrites would be very helpful.

Thanks in advanced.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will help you get started:
B4X:
Sub Process_Globals
   Dim parser As SaxParser
   Type Condition(day As String, low As Int, high As Int)
   Dim ListOfDays As List
   Dim workingCondition As Condition
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   parser.Initialize
   Dim in As InputStream
   in = File.OpenInput(File.DirAssets, "1.xml")
   ListOfDays.Initialize
   parser.Parse(in, "Parser")
   in.Close
   For i = 0 To ListOfDays.Size - 1
      Dim c As Condition
      c = ListOfDays.Get(i)
      Log(c)
   Next
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   Select Name
      Case "forecast_conditions"
         Dim workingCondition As Condition 'create a new object
         ListOfDays.Add(workingCondition)
      Case "day_of_week"
         workingCondition.day = Attributes.GetValue2("", "data")
      Case "low"
         workingCondition.low = Attributes.GetValue2("", "data")
   End Select
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   
End Sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Just the job

Hello Erel,
Thank you for the quick response, that's just what the doctor ordered :sign0060:

The above code worked a treat. I

will admit it though, I was not expecting it to be that complex just to read an XML feed from Google weather.

Anyway, thank you again for your quick response and answer...

Cheers :sign0098:
 
Upvote 0
Top