【问题标题】:TinyXML2 query text if attribute matches如果属性匹配,TinyXML2 查询文本
【发布时间】:2012-04-12 01:15:33
【问题描述】:

我正在尝试找出一种方法来从我使用 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>

基本上,我只想将&lt;data&gt; 中的文本复制到一个名为background 的字符串中,前提是图层名称为"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")-&gt;FirstChildElement("layer"),如果是== "Background",获取文本。

我将如何做到这一点?

【问题讨论】:

    标签: c++ xml tinyxml2


    【解决方案1】:

    我知道这个帖子已经很老了,但以防万一有人仔细阅读互联网可能会像我一样偶然发现这个问题,我想指出 Xanx 的答案可以稍微简化。

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

    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();
    }
    

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

    【讨论】:

      【解决方案2】:

      我建议你这样做:

      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();
      }
      

      【讨论】:

        【解决方案3】:
        auto bgData = text (find_element (doc, "map/layer[@name='Background']/data"));
        

        使用tinyxml2 extension (#include &lt;tixml2ex.h&gt;)。 注:真的应该被包裹在一个 try/catch 块中。 正在进行的工作和文档不完整(可以从测试示例中推断出来,直到准备好)。

        我会顺便提一下,其他两个答案只有在所需的 &lt;layer&gt; 元素首先出现时才能正常工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-10
          • 1970-01-01
          • 2021-05-21
          • 2011-01-01
          • 1970-01-01
          相关资源
          最近更新 更多