0

I'm using TinyXml2 v8.0.0 to create an XML buffer to send to an API. The example includes a declaration. I'm implementing this with:

XMLDocument doc;
doc.InsertEndChild(doc.NewDeclaration());
XMLElement* pRoot = doc.NewElement("Stuff");
doc.InsertFirstChild(pRoot);

The documentation for NewDeclaration states:

If the text param is null, the standard declaration is used.:

<?xml version="1.0" encoding="UTF-8"?>

You can see this as a test in https://github.com/leethomason/tinyxml2/blob/master/xmltest.cpp#L1637

But when I print the buffer out the declaration has been placed at the end of the buffer after a newline:

<Stuff>
</Stuff>

<?xml version="1.0" encoding="UTF-8"?>

Does anyone know why this is happening? I'd expect it to be at the start of the buffer with no newline.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
parsley72
  • 8,449
  • 8
  • 65
  • 98

1 Answers1

2

Presumably, that's because you told it to put the declaration as the EndChild and the Stuff element as the FirstChild.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I previously tried adding the declaration with `InsertFirstChild` but it didn't make a difference. However, your suggestion works. Maybe `InsertFirstChild` always adds to the start? – parsley72 Oct 20 '22 at 00:45
  • @parsley72 if you look it up, you will see that InsertFirstChild always adds to the start, and InsertEndChild always adds to the end. – user253751 Oct 20 '22 at 14:58