c++ - 如何使用tinyxml2解析XML文件并将属性保存到结构体当中?

浏览:37日期:2023-05-26

问题描述

我正在学习cocos2d-x游戏开发。现在我需要从一个已有的XML文件中提取出精灵元素的相关信息,比如“名字”,“形状”,“颜色”等等。我在代码中,声明了一个结构体,本意是用来存放提取出来的信息。现在遇到的问题是,如何访问其中的属性标签并保存到结构体当中呢?结构体代码如下:

typedef struct { std::string _color; std::string _shape; std::string _fileName;}fishInfo;

xml文件结构如图:

c++ - 如何使用tinyxml2解析XML文件并将属性保存到结构体当中?

请各位点拨一下。或者发一下相关的xml解析完全指南之类的教程?

问题解答

回答1:

不懂TinyXML, 那是什么鬼

pugi::xml_document doc;doc.load_file(’./file_name.xml’);pugi::xpath_node_set nodes = doc.select_nodes('/fishes/fish');for(pugi::xpath_node_set::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter) { pugi::xml_node node = iter->node(); std::string color = node.attribute('color').as_string(); std::string shape = node.attribute('shape').as_string(); std::string name = node.attribute('name').as_string();}

相关文章: