Search code examples
copenstreetmaplibxml2

how can i access to <tag key = ... value = .../> which is IN a <node > using libxml2?


I'm trying to parse an OSM file using libxml2. Here is a part of osm file:

   <node id="368138" lat="48.8546445" lon="2.3627305" user="Pieren"
    uid="17286" visible="true" version="3" changeset="4490579"
     timestamp="2010-04-21T20:34:49Z">
   <tag k="highway" v="traffic_signals"/>
   </node>

I'm need to get the key and value of the tag. But i don't know how. I tried to access using

xmlHasProp(node,(const xmlChar*)"tag")

but I think tag is not considered as a prop of a node.


Solution

  • The straightforward option is to walk from the root node to the descendent node you're looking for. Child nodes of an XML document or element can be accessed as a linked list via the children and next pointers. For example, assuming node is an OSM node element:

    for (xmlNode *cur = node->children; cur; cur = cur->next) {
        if (cur->type == XML_ELEMENT_NODE
            && xmlStrcmp(cur->name, BAD_CAST "tag") == 0) {
            /* Found "tag" element. */
        }
    }
    

    Another option is to use libxml2's XPath engine. It allows you enumerate nodes using an XPath expression.

    Have a look at the examples on the libxml2 website.