Reading XML Data

bishmedia

Member
Licensed User
Longtime User
I have to say for a newbie like myself there's not that much info for dummies for a simple read XML file to listview and select!! I have found some examples but they dont really explain the basics so i'm trying to chip away at an existing and here is my first question....

Im trying to read and xml file externally but it doesn't work..

This is OK -

B4X:
In = File.OpenInput(File.DirAssets, "xmldata.xml")

But this throws an error -

B4X:
In = File.OpenInput(File.DirAssets, "http://www.bishmedia.co.uk/default/Apps/xmldata.xml")

Anyone?
 

sorex

Expert
Licensed User
Longtime User
you entered an external/web link to an xml somewhere.

do also a search on XOM it's a lot easier than the SAX method which requires a lot of self written code to fetch data.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
then you need other code, search for httputil that contains ways to download web content
 
Upvote 0

bishmedia

Member
Licensed User
Longtime User
Phew that went straight over my head.....

Basically all im looking for are a couple of lines of code...

1) Obtain data from external xml as above
2) add data to listview
3) when user selects a message box pops up "You Selected"

I will do the rest, im just not clever enough to figure out how to get it externally from my website, all examples ive found only show internal!!!

No idea how to use httpUtils either, i thought this was gonna be easy!!!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

gregwa

Member
Licensed User
Longtime User
I threw together a VERY minimal program for you.

You have to include the following libraries...
Code (already selected)
HTTP
StringUtils
XmlSax

AND you need to include the following modules.
HttpJob and HttpUtils2Service.

The program will go out, grab the xml file from your example and create an InputList box that holds all the band names. Change the code to suit your need.

Gregwa

B4X:
'Libraries required...
' Core, Http, StringUtils,XmlSax
' Additional Modules
' HttpJob, HttpUtils2Service

Sub Process_Globals
   '-----------------------------
   Dim WebSearch As HttpJob
   '-----------------------------
   Dim parser As SaxParser
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 SiteToRead = "http://www.bishmedia.co.uk/default/Apps/xmldata.xml"
   Dim BandList As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebSearch.Initialize("WebSearch",Me)
   BandList.Initialize
   parser.Initialize
   WebSearch.Download(SiteToRead)
End Sub

Sub DisplayList
   Dim ListLength As Int
   Dim resp As Int
   '-------------------------------------
   ListLength = BandList.Size-1
   If ListLength > 0 Then
      For cntr = 0 To ListLength
         Log("BandName = " & BandList.Get(cntr))
      Next
   End If
   resp = InputList(BandList,"Pick a band",-1)
End Sub
   
Public Sub JobDone (Job As HttpJob)
   '-----------------------------
   Dim j As InputStream
   Dim res As Map
   '-----------------------------
   res.Initialize
   If Job.Success = True Then
      j = Job.GetInputStream
      parser.Parse(j, "parser")
      DisplayList
   End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   '-----------------------------
   Dim cnt As Int
   Dim bandname As String
   '-----------------------------
   If parser.parents.IndexOf("comp") > -1 Then
      If Name = "bandName" Then
         bandname = Text.ToString
         BandList.Add(bandname)
      End If
   End If
End Sub
 
Upvote 0

bishmedia

Member
Licensed User
Longtime User
This looks perfect but im having trouble with this bit ..

AND you need to include the following modules.
HttpJob and HttpUtils2Service.


Im getting the following when i run

Parsing code. Error
Error parsing program.
Error description: Unknown type: httpjob
Are you missing a library reference?
Occurred on line: 22
Dim WebSearch As HttpJob
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
If you download the source zip I suggested earler and unpack it, the two files are there. Then in your program go to projects ¦ add existing module and point to each file which will add them to your project. I'm on my phone but I think that's the menu items.
 
Upvote 0

bishmedia

Member
Licensed User
Longtime User
Yes that works, brilliant....many thanks :)
I have to say though it seems a very complicated way in just wanting to populate a listview :)
 
Upvote 0
Top