0

I want to do this:

  1. Load a file.
  2. Find an element in it with it's attribute value like I want to find the dog tag with colour as brown. &ltdog colour="brown"&gt
  3. Then I want to change the contents of the tag. For example: from &ltdog colour="brown"&gtBaow!&lt/dog&gt to &ltdog colour="brown"&gtWaow!&lt/dog&gt.

All this has to be done using TinyXML2. So far, I could only open the file:

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

It would be great if you could help me.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

0

OK, so you know how to load the file:

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

We do not know the syntax of your XML file. But you have to walk down throw the elements.

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

file.SaveFile("...");

As I mentioned, you have to parse the elements through to get to the correct node.


The online readme file also states:

Lookup information.

/* ------ 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 );
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164