TinyXML2 / C ++ - insert element

I wanted to serialize objects from XML, so I got TinyXML. However, I went with the new TinyXML2. The problem is I can't find a tutorial anywhere, so I just read the documentation. However, I seem to be stuck with adding an element to a document.

Can anyone tell me what is wrong with my code?

Here's my file content demo.xml

:

<?xml version="1.0" ?>
<Hello>World</Hello>

      

here's my method main()

:

#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2;

int main (int argc, char * const argv[]) 
{
   XMLDocument doc;
   if (doc.LoadFile("demo.xml") == XML_SUCCESS)
   {
      XMLNode *node = doc.NewElement("foo");
      doc.InsertEndChild(node);
      doc.SaveFile("demo2.xml");
   }
}

      

and finally, here is the file demo2.xml

:

<?xml version="1.0" ?>
<Hello>World</Hello>

<foo/>

      

Foo should look like this: <foo></foo>

But for some reason this is not so. Can someone explain why?

+3


source to share


2 answers


In fact, it shouldn't look like this. You don't put any data "in between" your tags <foo>...</foo>

. As such <foo/>

(note the forward slash) is the correct representation of what you have.



+4


source


in between if, you can change your code like this:



XMLElement *node = doc.NewElement("foo");
XMLText *text = doc.NewText("Another Hello!");    
node->LinkEndChild(text);     
doc.LinkEndChild(node);

doc.SaveFile("demo2.xml");

      

+4


source







All Articles