C++ TinyXML2如果属性匹配,则查询文本

C++ TinyXML2如果属性匹配,则查询文本,c++,xml,tinyxml2,C++,Xml,Tinyxml2,我试图找到一种方法,从我使用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

我试图找到一种方法,从我使用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>
这非常有效,因为我知道元素名和属性名。有没有一种方法可以说获取
doc.FirstChildElement(“map”)->FirstChildElement(“layer”)
,如果它
==“Background”
,则获取文本


我该如何做到这一点?

我建议您这样做:

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();
}
if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
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();
}
if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
XMLElement * node =  doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;

if (node->Attribute("name", "Background")) // no need for strcmp()
{
   value = node->FirtChildElement("data")->GetText();
}

我知道这个帖子已经很老了,但为了防止有人像我一样在互联网上偶然发现这个问题,我想指出,Xanx的答案可以稍微简化一下

tinyxml2.h
中,它表示对于函数
const char*属性(const char*name,const char*value=0)const
,如果
value
参数不为空,则函数仅在
value
name
匹配时返回。根据文件中的注释,该文件:

if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
可以这样写:

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();
}
if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
XMLElement * node =  doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;

if (node->Attribute("name", "Background")) // no need for strcmp()
{
   value = node->FirtChildElement("data")->GetText();
}
因此,提供的代码Xanx可以重写如下:

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();
}
if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
XMLElement * node =  doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;

if (node->Attribute("name", "Background")) // no need for strcmp()
{
   value = node->FirtChildElement("data")->GetText();
}

是的,有一点小小的改变,但我想补充一点。

我知道这个帖子很老了,但为了防止有人像我一样在互联网上读到这个问题,我想指出,Xanx的答案可以稍微简化一点

auto bgData = text (find_element (doc, "map/layer[@name='Background']/data"));
tinyxml2.h
中,它表示对于函数
const char*属性(const char*name,const char*value=0)const
,如果
value
参数不为空,则函数仅在
value
name
匹配时返回。根据文件中的注释,该文件:

if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
可以这样写:

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();
}
if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
XMLElement * node =  doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;

if (node->Attribute("name", "Background")) // no need for strcmp()
{
   value = node->FirtChildElement("data")->GetText();
}
因此,提供的代码Xanx可以重写如下:

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();
}
if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
XMLElement * node =  doc.FirstChildElement("map")->FirstChildElement("layer");
std::string value;

if (node->Attribute("name", "Background")) // no need for strcmp()
{
   value = node->FirtChildElement("data")->GetText();
}
是的,有点小变化,但我想补充一点

auto bgData = text (find_element (doc, "map/layer[@name='Background']/data"));
使用(
#include
)。 注意:应该用try/catch块包装。 正在进行的工作和文档不完整(可以从测试示例中推断,直到它准备就绪)

我顺便提一下,只有当所需的
元素首先出现时,其他两个答案才能正常工作

使用(
#include
)。 注意:应该用try/catch块包装。 正在进行的工作和文档不完整(可以从测试示例中推断,直到它准备就绪)

我顺便提一下,只有当所需的
元素首先出现时,其他两个答案才能正常工作