开发者

TinyXml Parser refuses to load file properly

I have the following xml file:

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

Which is located in the very same 开发者_如何转开发directory as all my other files.

And I use this source file method to parse it:

void Character::assign_xml(const char * filename) //Assign xml takes the name of the xml file as a string, and uses it to parse the file's nodes.
{
    TiXmlDocument * doc = new TiXmlDocument(filename);

    bool loadOkay = doc->LoadFile(filename);



    if (loadOkay)
    {
        printf("\n%s\n", filename);
    }
    else
    {
        printf("%s does not work.", filename);
    }

    delete doc;

}

Yet, when I pass the string to it, my loadOkay variable amounts to false. Why is this?

My output produces the following:

Starting /home/holland/code/qt/chronos-build-desktop/chronos...
id01.xml does not work.Failed to open file/home/holland/code/qt/chronos-build-desktop/chronos exited with code 0

Where as an strace provides:

futex(0x84579c, FUTEX_WAKE_PRIVATE, 2147483647) = 0
open("id01.xml", O_RDONLY)              = -1 ENOENT (No such file or directory)
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb773e000
write(1, "id01.xml does not work.Failed to"..., 42id01.xml does not work.Failed to open file) = 42


You shouldn't pass the filename both to the document constructor and to LoadFile(). Try omitting it from the latter, as per the examples on TinyXML's site.

If it still doesn't work, print out doc->ErrorDesc() (and possibly ErrorRow and ErrorCol).

And read the documentation: http://www.grinninglizard.com/tinyxmldocs/classTiXmlDocument.html


There's a small example of using TinyXML for parsing 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 your xml file (it worked for me) or a xml document like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜