I'm working on XML file (in particula .osm file) with libxml2 and C language.
This is my XML source (not all but just a piece):
...
< way id="9484424" visible="true" version="8" changeset="11179686" timestamp="2012-04-04T18:01:31Z" user="Davlak" uid="217070">
< nd ref="72843570"/>
< nd ref="963203594"/>
< nd ref="612588857"/>
< nd ref="300164764"/>
< nd ref="72843572"/>
< tag k="highway" v="unclassified"/>
< tag k="name" v="Via Giorgio Pitacco"/>
< tag k="odbl" v="clean"/>
< tag k="oneway" v="yes"/>
< /way>
< way id="11378242" visible="true" version="29" changeset="22876351" timestamp="2014-06-11T18:00:17Z" user="Davlak" uid="217070">
< nd ref="101181728"/>
< nd ref="2911704721"/>
< nd ref="2911704714"/>
< nd ref="101181731"/>
< tag k="foot" v="yes"/>
< tag k="highway" v="trunk_link"/>
< tag k="lanes" v="2"/>
< tag k="lit" v="yes"/>
< tag k="maxspeed" v="40"/>
< tag k="mofa" v="no"/>
< tag k="moped" v="no"/>
< tag k="motorroad" v="yes"/>
< tag k="name" v="Circonvallazione Tiburtina"/>
< tag k="oneway" v="yes"/>
< tag k="sidewalk" v="right"/>
< /way>
< way id="20507982" visible="true" version="11" changeset="17228559" timestamp="2013-08-05T14:00:29Z" user="Davlak" uid="217070">
< nd ref="219557958"/>
< nd ref="1920238894"/>
< nd ref="1701671630"/>
< nd ref="1920238892"/>
< nd ref="219557991"/>
< tag k="highway" v="residential"/>
< tag k="lit" v="yes"/>
< tag k="name" v="Via Prenestina"/>
< /way>
< way id="20507983" visible="true" version="14" changeset="26174383" timestamp="2014-10-18T17:12:55Z" user="Privits" uid="2009312">
< nd ref="219558042"/>
< nd ref="2276045993"/>
< nd ref="2023496104"/>
< nd ref="219558031"/>
< nd ref="2276045996"/>
< nd ref="922740441"/>
< nd ref="922740415"/>
< nd ref="3137149509"/>
< nd ref="1620785409"/>
< nd ref="219558021"/>
< nd ref="301486927"/>
< nd ref="298561544"/>
< tag k="highway" v="residential"/>
< tag k="lanes" v="1"/>
< tag k="name" v="Via Prenestina"/>
< tag k="oneway" v="yes"/>
< /way>
...
My output, right now , is (so using the following code i'm printing):
way: 9484424
node: 72843570
node: 963203594
node: 612588857
node: 300164764
node: 72843572
and so on for each way element...
I would like to print, for each way, ONLY the first and the last node, in the same line, and the way that these nodes connect. For example:
node: 72843570 node: 72843572 way: 9484424
This is my code actually:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int is_leaf(xmlNode * node) {
xmlNode * child = node->children;
while(child) {
if(child->type == XML_ELEMENT_NODE) return 0;
child = child->next;
}
return 1;
}
void print_xml(xmlNode * node, int indent_len) {
char *name= "way";
char *nd = "nd";
while(node) {
if(node->type == XML_ELEMENT_NODE) {
if(strcmp (node->name, name) ==0) {
printf("%*c%s: %s\n", indent_len*2, ' ', node->name, is_leaf(node)?xmlNodeGetContent(node):xmlGetProp(node, "id"));
}
}
if(node->type == XML_ELEMENT_NODE) {
if(strcmp (node->name, nd) ==0) {
printf("%*c%s: %s\n", indent_len*2, ' ', "node", is_leaf(node)?xmlNodeGetContent(node):xmlGetProp(node, "ref"));
}
}
print_xml(node->children, indent_len + 1);
node = node->next;
}
}
int main() {
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
doc = xmlReadFile("Casal.osm", NULL, 0);
if (doc == NULL) {
printf("Could not parse the XML file");
}
root_element = xmlDocGetRootElement(doc);
print_xml(root_element, 1);
xmlFreeDoc(doc);
xmlCleanupParser();
}
I'm not able to save or to store the first and last node and print every way during the scan. How can I modify my code? THank you everybody.
asked
05 Apr '18, 11:14
claudiodipil...
11●3●3●4
accept rate:
0%