Hi
I’m a newbie to OSM and wanted to use the xml data for my 3rd Year degree Computing project. I’m having trouble reading the tags (nodes) correctly and would appreciate any advice/assistance.
E.g; Is there a list of the tag definitions and the tree structure of the xml file anywhere or is there a standard xml setup for reading osm files. Or have I just used the wrong export (I think I just used the std “OpenStreetMap XML Data” selection)?
Hi Chris
Thanks for the details, but I’d already got that far and got the file.
It’s being able to read the file correctly thats the issue. I’m in Brighton and have downloaded a section of Brighton and surrounding area and have tags titled member, node, way etc, but a standard Java xml file-reader program is having problems with the tags. That’s why I asked if there was a ‘listing’ of the tags/nodes etc?
Is there something special/different between an osm file and a ‘standard’ xml file?
The OSM data format is indeed XML but the data is largely held in attributes.
Take the above snippet.
The area that the document is describing is defined in the bounds element’s attributes minlat, minlon, maxlat, maxlon.
The node element contains meta data in it’s attributes and has a child element, tag.
The tag element contains a key value pair in the attributes k and v respectively. This is the juicy stuff, like amenity:pub, and that’s the actual data you’re interested in I imagine.
Parsing the file and actually accessing the data does require that you have a little knowledge but that’s the case with any XML format. XHTML contains a title element, for example, and the browser knows to look for that so that it can display a page title.
XML may be self describing but you still have to instruct the parser which elements you want to access.
Hi
That’s really useful for what I’m trying to do, which is use the nodes/attributes (and probably elements of the tag data) to plot routes in a Java program.
Is there any further info anywhere on parsers, or what data a standard parser would need (I’m especially presuming I’m going to need elements of the meta data attributes, such as the node’s “id” and also the various elements of the ‘way’ tags)?
Also, is there a full list of the tag elements anywhere?
Do you mean a list of keys and values that the k and v attributes on the tag element can take?
If so, then no. OpenStreetMap is freeform allowing anyone to use whatever keys and values they like.
Hi Alex
Sorry to follow this up after such a gap but I thought I had this sorted!!
Following on from your description of the tags and elements, I’m still have severe problems accessing the values of the tags. I’m using Java and all the examples I’m working on work for “standard” xml file and I can access and read most things, but…
In the example below (from a map of Brighton)
</way
I can access the attributes of ie id, timestamp etc and, using ‘getChildNodes()’ I can count how many nd and tag elements there are in the example, but I cannot seem to view, access or otherwise report on the k and v values. These are obviously the most vital ones.
Have you got any advice on what I might be missing or otherwise how to access these k and v elements?
And using ChildNodes you can determine how many nd and tag and…
But then ChildNodes should contain the data too. There should be other operations available to handle ChildNodes. Please show us the returnvalue of getChildNodes().
By the way: you can better change the subject of this topic to How to import XML or How to parse XML as that is where you have problems with. You aren’t exporting anything.
Hi
I have a method printAtt4 as below, (and fullWayLst is a NodeList of all the ‘way’ elements from my map set). This was one of my attempts to get at the tag data, all others were equally unsuccessful.
public void printAtt4()
{
for (int s=0; s < fullWayLst.getLength(); s++)
{
Node aSet = fullWayLst.item(s);
if (aSet.getNodeType() == Node.ELEMENT_NODE)
{
Element test = (Element)aSet.getChildNodes();
Element lev1 = (Element)test.getChildNodes();
String res = test.getAttribute(“version”);
String res1 = lev1.getAttribute(“v”);
System.out.println("Version is “+res+” and tag is "+res1);
}
}
}
This method returns a long set (the number of lines in my NodeList) but all in the following format
– Version is 2 and tag is (ie blank)
So, it picks up the Version meta-attribute of the way node, but cannot get at the tag data. This is working on a ‘way’ node as per my earlier example.
test is an Element object and is local to the method
The method is not designed to return anything per se, just to test that I can access the data correctly, hence only output. Element test should be the return from aSet.getChildNodes() and lev1 should be (hopefully) the return from (Element)test.getChildNodes()
aSet is a single way Node for each cycle through the for loop; this is looping through the NodeList fullWayLst, which contains all way Nodes from mymap set