libxml2在XML文件上使用C语言

时间:2018-03-23 11:52:28

标签: c xml parsing printf libxml2

我使用libxml2和C语言处理XML文件(特别是.osm文件)。

这是我的XML源代码(并非只是一部分):

...
        <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>

 ...

我的输出现在是(所以使用以下代码我打印):

way: 9484424
 node: 72843570
 node: 963203594
 node: 612588857
 node: 300164764
 node: 72843572
每个元素的

等......

我想为每种方式打印同一行中的第一个和最后一个节点,以及这些节点连接的方式。例如:

node: 72843570 node: 72843572 way: 9484424

这实际上是我的代码:

#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();
} 

我无法保存或存储第一个和最后一个节点,并且在扫描期间无论如何都要打印。我该如何修改我的代码?谢谢大家。

0 个答案:

没有答案