Android Question How can I parse Nominatim.xml file from the open street map site?

eronquillo

Member
Licensed User
Longtime User
Hi Everyone! I can't seem to get past this one. I am attempting to use xmlsax to parse the following nominatim.xml file which has the following structure:

B4X:
<searchresults>
    a="a"
    b="b"
    <place>
                lat="latitude coordinate"
                lon="longitude coordinate"
                <pub>Awesome Pub </pub>
                <house_number>23</house_number>
                <road>Victoria Street Lane</road>
                <city>Toronto</city>
                <country>Canada</country>
    </place>
    <place>
                lat=latitude coordinate
                lon=longitude coordinate
                <pub>Next Pub </pub>
                <house_number>19</house_number>
                <road>Dundas Street Lane</road>
                <city>Toronto</city>
                <country>Canada</country>
    </place>
    <place>
                lat=latitude coordinate
                lon=longitude coordinate
                <pub>Cool Pub </pub>
                <house_number>43</house_number>
                <road>Queen Street</road>
                <city>Toronto</city>
                <country>Canada</country>
     </place>
</searchresults>

What I want to do is parse all the values (lat, lon,<pub>,<road>,<city>,<country>) in each <place> element into an object called "store" and then put each "store" object them into a list. However, xmlsax stops at the <searchresults> in parser_StartElement, not being able to get to the following <place> sub -elements in the .xml file. Can someone see any problems with my code?

This is the xmlsax code:

B4X:
Type osmpoint (name As String, house_number As String, road As String, lat As String, _
   lon As String)

Dim listofplaces As List
listofplaces.initialize

Sub parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   If Name = "searchresults" Then
     Log("Found the searchresults element")
     If Name = "place" Then
       Log("found the place element")
       store.Initialize
       store.lat = Attributes.GetValue2("", "lat")
       store.lon = Attributes.GetValue2("", "lon")
       ' listofplaces.Add(store) - don't need it here since I want to store the end elements too
     Else
       Log("could not find the <place> element in nominatim.xml")
     End If
   Else
     Log("Could not find the <searchresults> element in nominatim.xml")
   End If
End Sub

Sub parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If parser.Parents.IndexOf("place") > -1 Then
     Select Name
       Case "pub"
         store.Name = Text.ToString
         Log("grabbed the pub name in parser_EndElement")
       Case "house_number"
         store.house_number = Text.ToString
         Log("grabbed the house_number in parser_EndElement")
     Case "road"
         store.road = Text.ToString
         Log("grabbed the road in parser_EndElement")
     End Select
  End If
  If Name = "place" Then
  listofplaces.Add(store) 'add the parsed store info to listofplaces
   Else
     Log("could not find the <place> element in nominatim.xml") 
  End If
End Sub

Update - Sorry, I forgot to include an XML attachment. Hopefully it can be read in by the sax parser.


Thanks!
 

Attachments

  • example-nominatim-pub-result.zip
    1.6 KB · Views: 222
Last edited:

eronquillo

Member
Licensed User
Longtime User
Hi Erel. Those are example attributes that are part of the parent element in the attached xml. To make it easier, I'm attaching a .png image file to illustrate what I'm trying to extract from the file so I can place them within the custom datatype I call "store" Each attribute and child of a <place> element would be part of a "store" e.g. store.lat, store.lon, store.name, store.road. After that I add them to a list object. I've been at this for while now, but I really stuck. Any advice is appreciated. Thanks!
 

Attachments

  • xmlparsing.png
    xmlparsing.png
    33.1 KB · Views: 227
Last edited:
Upvote 0

eronquillo

Member
Licensed User
Longtime User
Sorry Erel. Here's what I mean. The a and b attributes do not exist in the file, they were only to serve as an example of the basic structure of the actual XML file. What I'm only looking to parse are the following under <place :
lat (attribute)
lon (attribute)
<pub>
<road>


B4X:
<?xml version="1.0" encoding="UTF-8"?>
<searchresults timestamp="Thu, 05 Jun 14 23:35:57 +0000" attribution="Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright" querystring="Pub" viewbox="-79.39,43.66,-79.37,43.65" polygon="false" exclude_place_ids="3792695,770112,969860,969889,1078576,4971473,16524872,9164944157" more_url="http://nominatim.openstreetmap.org/search?format=xml&amp;exclude_place_ids=3792695,770112,969860,969889,1078576,4971473,16524872,9164944157&amp;accept-language=en-US,en;q=0.5&amp;addressdetails=1&amp;viewbox=-79.39%2C43.66%2C-79.37%2C43.65&amp;q=Pub">
<place place_id="3792695" osm_type="node" osm_id="412398579" place_rank="30" boundingbox="43.6562805175781,43.6562843322754,-79.3788070678711,-79.3787994384766" lat="43.6562819" lon="-79.3788002" display_name="Imperial Pub, Victoria Street Lane, Church-Wellesley Village, Toronto, Ontario, M5B 2G4, Canada" class="amenity" type="pub" importance="0.101" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>Imperial Pub</pub>
<road>Victoria Street Lane</road>
<neighbourhood>Church-Wellesley Village</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5B 2G4</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="770112" osm_type="node" osm_id="273325243" place_rank="30" boundingbox="43.6525268554688,43.652530670166,-79.3806381225586,-79.3806304931641" lat="43.6525273" lon="-79.3806327" display_name="Duke of Richmond, James Street, Discovery District, Toronto, Ontario, M5B2H1, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>Duke of Richmond</pub>
<road>James Street</road>
<neighbourhood>Discovery District</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5B2H1</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="969860" osm_type="node" osm_id="281660371" place_rank="30" boundingbox="43.6505355834961,43.6505393981934,-79.3884735107422,-79.3884658813477" lat="43.6505363" lon="-79.3884696" display_name="The Rex Jazz &amp; Blues Bar, Queen Street West, Discovery District, Toronto, Ontario, M5H 1W7, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>The Rex Jazz &amp; Blues Bar</pub>
<road>Queen Street West</road>
<neighbourhood>Discovery District</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5H 1W7</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="969889" osm_type="node" osm_id="281663297" place_rank="30" boundingbox="43.6515426635742,43.6515464782715,-79.3772888183594,-79.3772811889648" lat="43.6515437" lon="-79.3772842" display_name="Beer Academy, 75, Victoria Street, Discovery District, Toronto, Ontario, M5C 2B1, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>Beer Academy</pub>
<house_number>75</house_number>
<road>Victoria Street</road>
<neighbourhood>Discovery District</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5C 2B1</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="1078576" osm_type="node" osm_id="292779328" place_rank="30" boundingbox="43.6545219421387,43.6545257568359,-79.379020690918,-79.3790130615234" lat="43.6545231" lon="-79.3790188" display_name="Martini Bar, Victoria Street, Church-Wellesley Village, Toronto, Ontario, M5B 2G4, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>Martini Bar</pub>
<road>Victoria Street</road>
<neighbourhood>Church-Wellesley Village</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5B 2G4</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="4971473" osm_type="node" osm_id="515618892" place_rank="30" boundingbox="43.6554985046387,43.6555023193359,-79.3803863525391,-79.3803787231445" lat="43.6555018" lon="-79.3803803" display_name="3 Brewers, Yonge Street, Discovery District, Toronto, Ontario, M5B 2H1, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>3 Brewers</pub>
<road>Yonge Street</road>
<neighbourhood>Discovery District</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5B 2H1</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="16524872" osm_type="node" osm_id="1518394808" place_rank="30" boundingbox="43.6503219604492,43.6503257751465,-79.388069152832,-79.3880615234375" lat="43.650324" lon="-79.388064" display_name="Wild Wing, Queen Street West, Discovery District, Toronto, Ontario, M5H 1W7, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>Wild Wing</pub>
<road>Queen Street West</road>
<neighbourhood>Discovery District</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5H 1W7</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
<place place_id="9164944157" osm_type="node" osm_id="2496903052" place_rank="30" boundingbox="43.6592178344727,43.6592216491699,-79.382438659668,-79.3824310302734" lat="43.6592201" lon="-79.3824312" display_name="Midtown Reds Tavern, Gerrard Street West, Discovery District, Toronto, Ontario, M5B 1R8, Canada" class="amenity" type="pub" importance="0.001" icon="http://nominatim.openstreetmap.org/images/mapicons/food_pub.p.20.png">
<pub>Midtown Reds Tavern</pub>
<road>Gerrard Street West</road>
<neighbourhood>Discovery District</neighbourhood>
<suburb>Toronto</suburb>
<city>Toronto</city>
<state>Ontario</state>
<postcode>M5B 1R8</postcode>
<country>Canada</country>
<country_code>ca</country_code>
</place>
</searchresults>
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This will help you get started:
B4X:
Sub Process_Globals
   Type osmpoint (name As String, house_number As String, road As String, lat As String, _
        lon As String)
   Dim tempPoint As osmpoint
   Dim points As List
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   points.Initialize
   Dim parser As SaxParser
   parser.Initialize
   Dim In  As InputStream = File.OpenInput(File.DirAssets, "example-nominatim-pub-result.xml")
   parser.Parse(In, "parser")
   Log(points)
End Sub

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   If Name = "place" Then
     Dim tempPoint As osmpoint '<---- very important
     tempPoint.Initialize
     tempPoint.lat = Attributes.GetValue2("", "lat")
     tempPoint.lon = Attributes.GetValue2("", "lon")
     points.Add(tempPoint)
   End If
End Sub

Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
   If Name = "road" Then
     tempPoint.road = Text.ToString
   End If
End Sub
 
Upvote 0

eronquillo

Member
Licensed User
Longtime User
Thank Erel! I greatly appreciate it. I will give this a try and then post the results once I'm done testing.

Edward
 
Upvote 0
Top