Android Question Best practice for storing nested structures

jamesnz

Active Member
Licensed User
Longtime User
I'm sure this is much more simple than I'm making it sound and it's 12 months since I've done much programming
Basically I am pulling xml data from a web api which is made of various nested structures.
I want to be able to group the data together and then iterate through it to do various things.
The specifics are as follows

The xml return contains a set of Motorways, each motorway has some stats in it's parent node
an id
a name
a reference
an average speed
nested inside this motorway is a series of sections which when linked together form the motorway.
Each section has stats like
section name
volume
occupancy
average speed

Each section also has a start and end location
startlocation
start id
start lat
start lng
endlocation
end id
end lat
end lng

I can get the api to send back all motorways( which takes about 20 seconds) or I can get it to send back individual motorways ( ideally I want the user to select some favourite motorways and just pull these every 5 minutes or so)

From here I want to display the average speeds of EACH motorway and then when the user clicks on that motorway I can show the speeds of each section of the motorway by way of a colour coded polyline on a google map.
Currently I've made classes for the motorway , the nested sections, and the further nested startand end locations and I'm using an xml parser . The xml parser populates the motorway and section objects and stores them in a map.

This works well for single motorways but if I download more than one it combines them all into the same map.
I'm confused on whether I should be storing these objects in lists or maps or should I have a list of maps or some other structure?
Is there a best practice method for storing nested data, I'm sure there must be.

the guts of my code is like so


B4X:
Sub ReadXml(Job As HttpJob)
    Label1.text="made it to readxml"
    Dim In As InputStream
    In = Job.GetInputStream
    parser.Parse(In,"Parser")
   
    In.Close
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
If parser.parents.Size >0 Then
    If parser.Parents.get(parser.Parents.Size-1) ="return" Then
       
        If Name = "id" Then
        m.id = Text.tostring
        End If
       
        If Name = "name" Then
        m.Name= Text.ToString
        End If
               
        If Name ="averageSpeed" Then 
        m.averageSpeed = Text.ToString
        End If
       
        If Name = "reference" Then
        m.reference = Text.ToString
    Log("motorway class : "&m)
        End If
   
    End If
End If

    If parser.parents.Size >0 Then
    If parser.Parents.get(parser.Parents.Size-1) ="segments" Then
       
        If Name = "averageOccupancy" Then
        Dim tempao As Int = Text.tostring
        s.averageOccupancy = Round2(tempao,1)
        End If
       
        If Name = "averageSpeed" Then
        Dim temp As Int = Text.tostring
        s.averageSpeed= temp
        End If
       
        If Name = "carriagewaySegmentId" Then
        s.carriagewaySegmentId = Text.ToString
        End If
       
        If Name ="id" Then 
        s.id = Text.ToString
        End If
       
        If Name = "lastReadingTime" Then
        s.lastReadingTime = Text.ToString
        End If
       
        If Name = "linkId" Then
        s.linkId = Text.ToString
        End If
       
        If Name = "linkSequence" Then
        s.linkSequence = Text.ToString
        End If
       
        If Name = "reliability" Then
        s.reliability = Text.ToString
        End If
           
        If Name = "sectionEnd" Then
        s.sectionEnd = Text.ToString
        End If
       
        If Name = "sectionLength" Then
        s.sectionLength = Text.ToString
        End If
       
        If Name = "sectionName" Then
        s.sectionName = Text.ToString
        End If
       
        If Name = "sectionOrder" Then
        s.sectionOrder = Text.ToString
        End If
           
        If Name = "sectionStart" Then
        s.sectionStart = Text.ToString
        End If
               
        If Name = "sectionTime" Then
        s.sectionTime = Text.ToString
        End If
       
        If Name = "lastReadingTime" Then
        s.lastReadingTime = Text.ToString
        End If
       
        If Name = "totalVolume" Then
        Dim tempvol As Int = Text.ToString
                s.totalVolume =Round2(tempvol,1)*2
                Log("segment class "&s) ' LOG
                Log("******END OF SECTION******************************")
                ' add objects to map
               
                Main.mapofclasses.Put(("s"&count),s)
                Main.mapofclasses.Put(("sl"&count),sl)
                Main.mapofclasses.Put(("el"&count),el)
        '        Log(s)
        '        Log(sl)
        '        Log(el)
               
                count = count+1
        '        Log("count =" & count)
            'create new objects
            Dim bs As segment
            Dim bsl As startlocation
            Dim bel As endlocation
            ' assign clear objects 
            s = bs
            sl = bsl
            el = bel
        End If 
   
    End If

End If
   
   
    If parser.parents.Size >0 Then
    If parser.Parents.get(parser.Parents.Size-1) ="startLocation" Then
       
        If Name = "description" Then
        sl.description = Text.tostring
        End If
       
        If Name = "id" Then
        sl.id= Text.ToString
        End If
       
        If Name = "latitude" Then
        sl.latitude = Text.ToString
        End If
       
        If Name ="longitude" Then 
        sl.longitude = Text.ToString
        End If
           
        If Name ="speedLimit" Then 
        sl.speedLimit = Text.ToString
        'Log("start location " & sl)
        End If
    End If
    End If

    If parser.parents.Size >0 Then
    If parser.Parents.get(parser.Parents.Size-1) ="endLocation" Then
       
        If Name = "description" Then
        el.description = Text.tostring
        End If
       
        If Name = "id" Then
        el.id= Text.ToString
        End If
       
        If Name = "latitude" Then
        el.latitude = Text.ToString
        End If
       
        If Name ="longitude" Then 
        el.longitude = Text.ToString
        End If
           
        If Name ="speedLimit" Then 
        el.speedLimit = Text.ToString
        'Log("end location" & el)
        End If
    End If
    End If
End Sub
 
Top