Search code examples
c++tinyxml

TinyXML2 doesn't seem to load my file correctly?


I am just starting to use TinyXML2 so I am probably doing something wrong. Anyway:

tinyxml2::XMLDocument txDoc;
tinyxml2::XMLElement *rootnode;

XMLError err = txDoc.LoadFile(xmlFile.c_str()); // err says no error.

rootnode = txDoc.FirstChildElement("common");

rootnode is still set to a null pointer after the final line. I assume this is because it cannot find "common".

Here is my XML (shortened):

<?xml version="1.0"?>

<font>

<info outline="0" spacing="1,1" padding="0,0,0,0" aa="1" smooth="1" stretchH="100" unicode="1" charset="" italic="0" bold="0" size="16" face="Arial"/>

<common blueChnl="0" greenChnl="0" redChnl="0" alphaChnl="1" packed="0" pages="1" scaleH="128" scaleW="256" base="13" lineHeight="16"/>

<pages>
<page file="Font_Arial_16_0.png" id="0"/>
</pages>

<chars count="191">
... (removed additional <char>'s)
<char id="32" chnl="15" page="0" xadvance="4" yoffset="0" xoffset="0" height="16" width="1" y="85" x="121"/>
... (removed additional <char>'s)
</chars>

<kernings count="70">
... (removed additional <kerning>'s)
<kerning amount="-1" second="65" first="32"/>
... (removed additional <kerning>'s)
</kernings>

</font>

However, in the XMLDocument txDoc, the charBuffer only contains:

<?xml version="1.0"?>
<font

And apparently nothing more. So I assume it says there is no error because it finds and opens the file, but doesn't seem to get everything that's inside it.

Does anyone have any ideas? I am using TinyXML2, not 1.

I get the impression I am navigating the file incorrectly.


Solution

  • For the XMLDocument, FirstChildElement() is equivalent to RootElement() and your root element here is font. You want to call FirstChildElement() of the root element.