0

我想做这个:

  1. 加载文件。
  2. 在其中找到一个具有属性值的元素,就像我想找到颜色为棕色的狗标签一样。<dog colour="brown">
  3. 然后我想更改标签的内容。例如:从<dog colour="brown">Baow!</dog><dog colour="brown">Waow!</dog>

所有这些都必须使用 TinyXML2 来完成。到目前为止,我只能打开文件:

XMLDocument file; file.LoadFile("file.xml");

如果你能帮助我,那就太好了。

4

1 回答 1

0

好的,所以你知道如何加载文件:

XMLDocument file;
file.LoadFile("file.xml");

我们不知道您的 XML 文件的语法。但是你必须走下来扔掉元素。

XMLElement *pDog = file.FirstChildElement("dog");
if(pDog != null)
{
    if(pDog->Attribute("colour") == "brown)
    {
        pDog->SetText("Waow!");
    }
}

file.SaveFile("...");

正如我所提到的,您必须解析元素才能到达正确的节点。


在线自述文件还指出:

查找信息。

/* ------ Example 2: Lookup information. ---- */
{
  XMLDocument doc;
  doc.LoadFile( "dream.xml" );

  // Structure of the XML file:
  // - Element "PLAY"      the root Element, which is the
  //                       FirstChildElement of the Document
  // - - Element "TITLE"   child of the root PLAY Element
  // - - - Text            child of the TITLE Element

  // Navigate to the title, using the convenience function,
  // with a dangerous lack of error checking.
  const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
  printf( "Name of play (1): %s\n", title );

  // Text is just another Node to TinyXML-2. The more
  // general way to get to the XMLText:
  XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
  title = textNode->Value();
  printf( "Name of play (2): %s\n", title );
}
于 2018-04-07T07:45:29.933 回答