Help needed in parsing....please.

ciprian

Active Member
Licensed User
Longtime User
Parsing and ListView problems.

Hello. First of all, i'm a real newbie, so....i need your help if you have some minutes for me.
I am trying to do my first app and i need to parse an xml file situated in a webserver, and to place all the variables into a listview.
I have allready tried to understand the httputils exemple, and also the flickerviewer exemple...but i'm kinda zero into programation...i'm learning all by my self. So please help me.

The xml file looks like this...
<response>
<info0>0</info0>
<info1>0</info1>
<info2>0</info2>
<info3>0</info3>
<info4>0</info4>
<info5>0</info5>
<info6>0</info6>
<info7>0</info7>
</response>

And i have to download it from a webserver like... 192.168.2.50/status.xml.

Thank you all:sign0085:
 
Last edited:

ciprian

Active Member
Licensed User
Longtime User
Hello.
Here is my code...i have studied httputils2 and xmlsax tutorials....but i still have some errors.
Can you tell me what's wrong? i'm a newbie... :p
 

Attachments

  • NEW_Test_Cip.zip
    10.7 KB · Views: 200
  • status.txt
    71 bytes · Views: 241
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
That URL http://192.168.2.50:80/status.xml

would be on your own internal network so we can't access it.

Could you put it on a server on the internet?

Or supply us with an example of what the xml file should look like and I'll put it on my server for you.

added: Oh sorry, that 'status.txt' represents the xml file?

Ok, for anyone wishing to test I've put that on my server so change the above URL to:

http://btinterface.com/status.xml
 
Last edited:
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Here's the first part, getting the status.xml from the web.

Now we just need to figure out how to pass this to a XmlSax object and get the individual elements and put them into the ListView...

B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim URL As String = "http://BTInterface.com/status.xml"
   Dim Job1 As HttpJob
   Dim inString As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("LV1")
   Activity.Title = "WebXML"
   Job1.Initialize("Job1", Me)
   GoGetEm
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GoGetEm
   ProgressDialogShow("Fetching status.xml" )
   Job1.Download(URL)   
End Sub

Sub Button1_Click 'Refresh
   GoGetEm
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job1"
                'print the result to the logs
                inString = Job.GetString 
            Log(inString)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
   ProgressDialogHide
End Sub
 

Attachments

  • WebXML.zip
    9.2 KB · Views: 209
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Ok, this works :)

B4X:
#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim URL As String = "http://88.165.185.210:8010/status.xml"
   Dim Job1 As HttpJob
   Dim inString As String
   Dim myParser As SaxParser
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   Activity.LoadLayout("LV1")
   Activity.Title = "WebXML"
   Job1.Initialize("Job1", Me)
   myParser.Initialize
   GoGetEm
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GoGetEm
   ProgressDialogShow("Fetching status.xml" )
   Job1.Download(URL)   
End Sub

Sub Button1_Click 'Refresh
   GoGetEm
End Sub

Sub inParsing_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If myParser.Parents.IndexOf("response") > -1 Then
        If Name.StartsWith("led") Then
           ListView1.AddSingleLine2(Name & " = " & Text, Text) 
         Log(Uri & " : " & Name & " : " & Text)
      End If
    End If
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job1"
                'print the result to the logs
                inString = Job.GetString 
            Log(inString)
            myParser.Parse(Job.GetInputStream, "inParsing")
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
   ProgressDialogHide
End Sub

and try this line:
B4X:
        If Name.StartsWith("led") OR Name.StartsWith("btn") OR Name.StartsWith("an") OR Name.StartsWith("day") OR Name.StartsWith("time") Then

to get more stuff.
 

Attachments

  • WebXML.zip
    9.4 KB · Views: 190
Last edited:
Upvote 0

ciprian

Active Member
Licensed User
Longtime User
You are the best IanMc. It seems so easy for you....i can see that you have years of practice....i'm a litle player.. :)
I have to play fair from here....i have a lot to learn...

Thankyou
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Luckily B4a makes it fun to learn :)

Just think, you can make an app and put it on the Google Play Store and Amazon to be potentially viewed by gazzillions of people :)

Incentive enough to get learning I think.
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
of course you can just omit the If statement to get everything:

B4X:
Sub inParsing_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If myParser.Parents.IndexOf("response") > -1 Then
        'If Name.StartsWith("led") OR Name.StartsWith("btn") OR Name.StartsWith("an") OR Name.StartsWith("day") OR Name.StartsWith("time") Then
           ListView1.AddSingleLine2(Name & " = " & Text, Text) 
      'End If
      Log(Uri & " : " & Name & " : " & Text)
    End If
End Sub
 
Upvote 0

ciprian

Active Member
Licensed User
Longtime User
Very nice....from now, i will play with variables from xml, and will change button colors each time a variable gets change from 0 to 1 or from up to down.
I have to study that.....a long road to do...but thanx to you and the others...it's fun to learn.
 
Upvote 0
Top