XOM to Multiple Array or List

bishmedia

Member
Licensed User
Longtime User
I am calling from an XML using XOM but i would like to add the postcode/town/county attributes to a multiple array or list, perhaps before Spinner1.add(ListTitle) ??
Can anyone help me with a global array???

B4X:
<postc>
   <postcode>AB10</postcode>
   <town>Aberdeen</town>
   <county>Aberdeen</county>
</postc>

B4X:
Sub XOMBuilder1_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
   If XOMDocument1=Null Then
      '   XOMDocument1 will be Null if an error has occurred
      Log("An error has occured and the XOMDocument has NOT been created")
      '   now handle the failure to get and parse the XML
      
   Else
      Log("XOMDocument successfully created")
      Dim RootElement As XOMElement=XOMDocument1.RootElement   '   this will be the <datas> element/tag
      Dim CompElement As XOMElement=RootElement.GetFirstChildElementByName("result")
      Dim ResultElements As XOMElements=CompElement.GetChildElementsByName("postc")
      BuildSpinner
   End If   
End Sub

Sub BuildSpinner
   Dim i As Int
   Dim ListTitle As String
   Dim ResultElement As XOMElement
   
   For i=0 To ResultElements.Size-1
      ResultElement=ResultElements.GetElement(i)      
      ListTitle=ResultElement.GetFirstChildElementByName("postcode").value
      Spinner1.add(ListTitle)      
   Next   
End Sub
 

bishmedia

Member
Licensed User
Longtime User
Hi Erel

The structure of the XML is as follows, this only shows 1 record but the original (http://www.bishmedia.co.uk/default/Apps/postcodes.xml) has nearly 3000 records.

B4X:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datas xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <result>
   <postc>
      <postcode>AB10</postcode>
      <town>Aberdeen</town>
      <county>Aberdeen</county>
   </postc>
        </result?
</datas>

I'm trying to get users to type a postcode into something like
txtSearch_TextChanged or type in postcode and click a 'Find Button' and then the 'Town' and 'County' text boxes are automatically filled in and i thought a Multiple Array might be easier??
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this (untested) code:
B4X:
Sub Process_Globals
   Dim parser As SaxParser
   Type PostC(PostCode As String, Town As String, County As String)
   Dim posts As List
   Dim tempPost As PostC
End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Parse(In As InputStream)
   parser.Initialize
   posts.Initialize
   parser.Parse(In, "parser")
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   If Name = "postc" Then
      Dim tempPost As PostC
      tempPost.Initialize
   End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   Select Name
      Case "postcode"
         tempPost.PostCode = Text
      Case "town"
         tempPost.Town = Text
      Case "county"
         tempPost.County = Text
      Case "postc"
         posts.Add(tempPost)
   End Select
End Sub
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi again.

Try this, it's much like the example code i posted in your other thread but uses a List and Type to save the parsed XML.
Storing the data in a List using a Type allows you to sort the List by one or more of the Type fields:

B4X:
Sub Process_Globals
   Type Postcode( _
      County As String, _
      Postcode As String, _
      Town As String)
   Dim Postcodes As List
End Sub

Sub Globals
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ListView1.Initialize("ListView1")
   ListView1.FastScrollEnabled=True
   Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
   
   If Postcodes.IsInitialized=False Then
      Dim XOMBuilder1 As XOMBuilder
      XOMBuilder1.Initialize("XOMBuilder1")
      XOMBuilder1.BuildFromURL("http://www.bishmedia.co.uk/default/Apps/postcodes.xml", Null)
   Else
      BuildListView
   End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub XOMBuilder1_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
   If XOMDocument1=Null Then
      '   XOMDocument1 will be Null if an error has occurred
      Log("An error has occured and the XOMDocument has NOT been created")
      '   now handle the failure to get and parse the XML
      
   Else
      Log("XOMDocument successfully created")
      Dim PostcElement As XOMElement=XOMDocument1.RootElement.GetFirstChildElementByName("result")
      Dim PostcElements As XOMElements=PostcElement.GetChildElementsByName("postc")
      Dim PostcCount As Int=PostcElements.Size
      Dim PostcElement As XOMElement
      Postcodes.Initialize
      Dim i As Int
      For i=0 To PostcCount-1
         PostcElement=PostcElements.GetElement(i)
         Dim Postcode1 As Postcode
         Postcode1.Initialize
         Postcode1.County=PostcElement.GetFirstChildElementByName("county").Value
         Postcode1.Postcode=PostcElement.GetFirstChildElementByName("postcode").Value
         Postcode1.Town=PostcElement.GetFirstChildElementByName("town").Value
         Postcodes.Add(Postcode1)
      Next
      
      BuildListView
   End If
   
End Sub

Sub BuildListView
   Dim i As Int
   Dim ListTitle As String
   Dim Postcode1 As Postcode
   
   '   sort the List of Postcode Type by a Postcode field
   Postcodes.SortType("Town", True)
      
   For i=0 To Postcodes.Size-1
      Postcode1=Postcodes.Get(i)
      
      ListTitle=Postcode1.Town&", "&Postcode1.County&" ("&Postcode1.Postcode&")"
      
      ListView1.AddSingleLine2(ListTitle, Postcode1)
   Next
   
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
   Log("ListView1_ItemClick")
   
   Dim Postcode1 As Postcode=Value
   
   Log(Postcode1.Postcode&" "&Postcode1.Town&" "&Postcode1.County)
   
   Log("********************")
End Sub

Martin.
 

Attachments

  • get_postcodes.zip
    6.3 KB · Views: 288
Upvote 0

bishmedia

Member
Licensed User
Longtime User
How would this work if say I had a postcode text box and as the user typed into txtPostcode the text change event looks up the list and placed the town in txtTown and County in TxtCounty??
I only ask this as the postcode list box has 1000's of locations and I thought that would be a pain if your postcode was say 'w'
I remember reading a post somewhere that 'text change' uses New and Old ??

Sorry I'm on my phone but I hope you understand what I mean :)
 
Upvote 0
Top