tinyxml2 load xml errorID 10

I'm working on a project in C++ using tinyxml2. I have an issue with the xml parsing , I get an errorID 10 and the error message is "XML_ERROR_PARSING_TEXT" when the file is loaded.

This is the following xml in question:

<Game>
  <Window>
    <width>600</width>
    <height>500</height>
    <background>joliBackgroundDeGael.jpg</background>
  </Window>
  <Square>
    <Mario>
      <size>
        <width>30</width>
        <height>15</height>
      </size>
      <speedPerFrame>5</speedPerFrame>
      <font>
        <stop>stopMario.jpg</stop>
        <run>runMario.jpg</run>
        <jump>jumpMario.jpg</jump>
      </font>
    </Mario>
  </Square>
</Game>

The xml file is valid in the W3C validator. So I think the problem is not here, but maybe here :

void parseXML::getDoc()
{
    this->doc.LoadFile(this->path);
    if (this->doc.ErrorID() != 0)
    {
        printf("load file=[%s] failed\n", this->doc.GetErrorStr1());
        printf("load file=[%s] failed\n", this->doc.GetErrorStr2());
    }
}

When I look in the LoadFile function with a breakpoint, I see that the load file is the same as below.

Here the complete code :

#include "caracteristique.h"
#include <iostream>

#include <direct.h>
#define GetCurrentDir _getcwd

using namespace tinyxml2;

const char* parseXML::path = "XMLType.xml";

void parseXML::getDoc()
{
    this->doc.LoadFile(this->path);
    if (this->doc.ErrorID() != 0)
    {
        printf("load file=[%s] failed\n", this->doc.GetErrorStr1());
        printf("load file=[%s] failed\n", this->doc.GetErrorStr2());
    }
}

int parseXML::getWindowHeight()
{
    if (this->doc.Error())
        this->getDoc();

    XMLElement *root = this->doc.RootElement();
    if (!root)
    {
        XMLElement *window = root->FirstChildElement("Window");
        if (!window)
            std::cout <<  window->FirstChildElement("height")->FirstChild()->ToText() << std::endl;
    }
    return 0;
}

An idea ?

Thanks for your help,

Upvotes: 0

Views: 4207

Answers (1)

Chewbacca17
Chewbacca17

Reputation: 61

Don't forget that the LoadFile method load AND parse your file. If your file doesn't follow xml standards, the method will fail and return FALSE. You should verify that none of your xml attributes contains special caracters like (,),/,\ for exemple. There's a small example on that page: Tiny XML Parser Example

Here's the exemple with tiny modifications:

#include "tinyxml.h" 

#include <iostream>
#include <string>
using namespace std; 

void Parcours( TiXmlNode* node, int level = 0 )
{
    cout << string( level*3, ' ' ) << "[" << node->Value() << "]";
    if ( node->ToElement() )
    {
        TiXmlElement* elem = node->ToElement();
        for ( const TiXmlAttribute* attr = elem->FirstAttribute(); attr; attr = attr->Next() )
            cout << " (" << attr->Name() << "=" << attr->Value() << ")";
    }
    cout << "\n";     

    for( TiXmlNode* elem = node->FirstChild(); elem; elem = elem->NextSibling() )
        Parcours( elem, level + 1 );
} 

int main( int argc, char* argv[] )
{
    TiXmlDocument doc("C:/test.xml" );
    bool loadOkay = doc.LoadFile();
    if ( !loadOkay ) {
        cerr << "Could not load test file. Error='" << doc.ErrorDesc() << "'. Exiting.\n";
        return 1;
    } 
    Parcours( doc.RootElement() );
}

You could try it with a xml document like this:

<Parent>
    <Child1 test="program" />    
    <Child2>
        <Node number="123456789" />      
    </Child2>
    <Child3>        
        <Hello World="!!!" />
    </Child3>    
</Parent>

I tried it and it worked well, you only have to execute the code passing the name of the file in the first argument.

Upvotes: 1

Related Questions