使用eclipse在C++中读取openstreetmap XML文件

Reading a openstreetmap XML file in C++ using eclipse

本文关键字:openstreetmap XML 文件 读取 eclipse C++ 使用      更新时间:2023-10-16

我是xml的新手。我正在尝试读取一个从openstreetmaps中获取的xml文件。它看起来像

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
 <bounds minlat="48.3994690" minlon="9.9897780" maxlat="48.3998220" maxlon="9.9898260"/>
 <node id="149248718" lat="48.3994984" lon="9.9897961" user="Matthias Frank" uid="287306" visible="true" version="7" changeset="7966729" timestamp="2011-04-25T18:01:43Z"/>
 <node id="1831376336" lat="48.3995041" lon="9.9898087" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:51Z"/>
 <node id="1831376346" lat="48.3995599" lon="9.9902392" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:52Z"/>
 <node id="1831376331" lat="48.3993243" lon="9.9898198" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:51Z"/>
 <node id="1831376349" lat="48.3996127" lon="9.9906471" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:52Z"/>
 <node id="1831376348" lat="48.3996044" lon="9.9910549" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:52Z"/>
 <node id="1831376418" lat="48.4000685" lon="9.9897909" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:55Z"/>
 <node id="1831376350" lat="48.3996158" lon="9.9908451" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:52Z"/>
 <node id="1831376374" lat="48.3999450" lon="9.9897917" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:54Z"/>
 <way id="172207897" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:02:59Z">
  <nd ref="1831376348"/>
  <nd ref="1831376350"/>
  <nd ref="1831376349"/>
  <nd ref="1831376346"/>
  <nd ref="1831376336"/>
  <tag k="bicycle" v="yes"/>
  <tag k="highway" v="living_street"/>
  <tag k="name" v="Hintere Rebengasse"/>
  <tag k="oneway" v="yes"/>
 </way>
 <way id="172207928" user="NUP" uid="168758" visible="true" version="1" changeset="12345652" timestamp="2012-07-19T20:03:01Z">
  <nd ref="1831376331"/>
  <nd ref="1831376336"/>
  <nd ref="1831376374"/>
  <nd ref="1831376418"/>
  <tag k="bicycle" v="yes"/>
  <tag k="highway" v="living_street"/>
  <tag k="name" v="Pfauengasse"/>
  <tag k="oneway" v="yes"/>
 </way>
</osm>

读取数据的最简单和最短的方法是什么?例如,从节点中,我想读取id、lat、lon,对于每个节点,我也想保存这些信息。对于作为"方式"的元素,我想阅读附在它上面的ref和"标签"元素。

我使用的是简单的fstream,但它看起来不是最有效的方式。

请帮助

您可以也应该使用XML解析器:

http://lars.ruoff.free.fr/xmlcpp/

对于轻量级解析,我通常使用RapidXml。它快速且易于使用(至少从我的角度来看)

您应该使用XML解析库。哪一个取决于你。目前,我正在研究tinyxml,但我还没有使用它,所以我不能支持它。不过,它看起来确实很简单,可以合并到一个项目中。