C++ What is the most efficient way of reading XML files?

时间:2015-07-28 22:07:45

标签: c++ xml parsing

I'm quite interested in trying to figure out how to read XMLfiles as it seems like the optimal way to create levels for my video game. I am using a program called pyxel edit to design the levels and I can export the sprite sheet as an XML.

  • It's a 2d sprite sheet so the x value indicates the sprites position on the x axis and the y value indicates the sprites position on the y axis.
  • List item

The output looks like this:

<tilemap tileswide="2" tileshigh="2" tilewidth="16" tileheight="16">
   <layer number="0" name="Layer 0">
      <tile x="0" y="0" index="0" rot="0" flipX="false"/>
      <tile x="1" y="0" index="1" rot="0" flipX="false"/>
      <tile x="0" y="1" index="2" rot="0" flipX="false"/>
      <tile x="1" y="1" index="3" rot="0" flipX="false"/>
   </layer>
</tilemap>

I started out by learning javascipt and C# for Unity3D but I never found myself in a situation where XML would be useful.

Any good online resources I can use to improve my understanding of it?

It would also help me out greatly if anyone has the time to provide an example on how to read this but that is only if you have the time to do so. If not then I'll work with any resources I find :)

2 个答案:

答案 0 :(得分:2)

Use an existing library.

A popular one is TinyXML-2, which is trivial to include as a dependency (single .cpp file + header), is fast, and easy to use.

You can find an example in the on-line documentation.

答案 1 :(得分:0)

对于解析XML文件,也许boost library是一个非常好的选择。解析XML文件很方便。有关详细信息,请单击boost/property_tree/xml_parser.hpp。我希望这可以帮到你。