TinyXML2 查询文本(如果属性匹配)
TinyXML2 query text if attribute matches
我正在尝试找出一种方法,从我使用TinyXML2创建的XML文档中加载文本。这是整个文档。
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="15" height="13" tilewidth="32" tileheight="32">
<tileset firstgid="1" name="Background" tilewidth="32" tileheight="32">
<image source="background.png" width="64" height="32"/>
</tileset>
<tileset firstgid="3" name="Block" tilewidth="32" tileheight="32">
<image source="block.png" width="32" height="32"/>
</tileset>
<layer name="Background" width="15" height="13">
<data encoding="base64">
AgAAAAIAAAACAAAA...
</data>
</layer>
<layer name="Block" width="15" height="13">
<data encoding="base64">
AwAAAAMAAAADAAAAAwAAAAM...
</data>
</layer>
</map>
基本上,仅当图层名称为 "Background"
时,我才想将文本从 <data>
复制到名为 background
的字符串中。
我得到了其他变量,如下所示:
// Get the basic information about the level
version = doc.FirstChildElement("map")->FloatAttribute("version");
orientation = doc.FirstChildElement("map")->Attribute("orientation");
mapWidth = doc.FirstChildElement("map")->IntAttribute("width");
mapHeight = doc.FirstChildElement("map")->IntAttribute("height");
这很好用,因为我知道元素名称和属性名称。有没有办法说得到doc.FirstChildElement("map")->FirstChildElement("layer")
,如果== "Background"
,得到文本。
我将如何实现这一点?
我知道这个线程很旧,但以防万一有人仔细阅读互联网可能会像我一样偶然发现这个问题,我想指出 Xanx 的答案可以稍微简化一下。
在tinyxml2.h
中,它说对于函数const char* Attribute( const char* name, const char* value=0 ) const
,如果value
参数不为空,则函数仅在value
和name
匹配时才返回。根据文件中的注释,这:
if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
可以这样写:
if ( ele->Attribute( "foo" ) ) {
if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
所以 Xanx 提供的代码可以这样重写:
XMLElement * node = doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;
if (node->Attribute("name", "Background")) // no need for strcmp()
{
value = node->FirtChildElement("data")->GetText();
}
一个小小的变化,是的,但我想补充一些东西。
我建议你做这样的事情:
XMLElement * node = doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;
// Get the Data element's text, if its a background:
if (strcmp(node->Attribute("name"), "Background") == 0)
{
value = node->FirtChildElement("data")->GetText();
}
auto bgData = text (find_element (doc, "map/layer[@name='Background']/data"));
使用 tinyxml2 扩展名 ( #include <tixml2ex.h>
)。注意:真的应该被包裹在一个尝试/捕获块中。正在进行的工作和文档不完整(可以从测试示例中推断出来,直到准备就绪)。
我会顺便提一下,其他两个答案只有在首先出现所需的<layer>
元素时才能正常工作。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 如果着色器中未使用绑定属性位置,是否会对其进行惩罚
- 对于属性上的 NOTIFY 信号,如果我给它一个参数有什么区别?
- 如何使用boost::graph dijkstra的算法,如果顶点属性是指针?
- 如果使用类的指针属性来访问堆,如何避免非标量类型的错误
- GCC 错误:如果已知函数正常返回,则函数可能是属性"pure"的候选者
- 如果第二个属性未支撑,则将与支撑初始化器配对缩小转换
- 如何声明 noexcept 如果只有属性的成员函数是 noexcept
- 如果我在一个类中有一个静态属性,并创建该类的许多新实例
- 如果我希望子类向父类添加功能,我是否应该保护我的属性
- 如果复合模式中的每个复合项都有其自己的许多独特属性,是否可以
- TinyXML2 查询文本(如果属性匹配)
- Qt和OpenGL,如果我使用属性,可以画一个三角形
- 如果有任何注释附加到键值,Boost属性树ini解析会出错
- 如果某些实例不使用基类的属性,我应该继承子类吗?
- 如果在目录路径的stat()失败时解释stat()属性,会发生什么?
- 如果需要声明具有所有属性的类的const实例
- 错误C2512:没有合适的默认构造函数可用-如果在构造函数中初始化属性,为什么