开发者

RapidXML is throwing exception

     ifstream fin("tree.xml");
    if (fin.fail()) return 1;

    fin.seekg(0, ios::end);
    size_t length = fin.tellg();开发者_如何转开发
    fin.seekg(0, ios::beg);
    char* buffer = new char[length + 1];
    fin.read(buffer, length);
    buffer[length] = '\0';

    fin.close();

    xml_document<> doc;
    doc.parse<parse_full>(buffer);

 //   doc.parse<0>(buffer);

    delete [] buffer;

    cout << "The first node is '" << doc.first_node()->name() << "'\n";
    for (xml_node<>* n = doc.first_node("card")->first_node(); n;
        n = n->next_sibling())
    {
        char* v = n->value();
        if (!v || !*v) v = "(empty)";
        cout << n->name() << " : " << v << '\n';
    }

This is the code which i have written for XML parsing using RapidXML, but it throws exception "rapidxml::parse_error at memory location 0x0011fc20.." Please suggest any fix for this. Thanx


You may be able to nail down exactly what is causing this by looking at this link http://rapidxml.sourceforge.net/manual.html#classrapidxml_1_1parse__error

In particular (bold text is my emphasis)

class rapidxml::parse_error

Defined in rapidxml.hpp

Description

Parse error exception. This exception is thrown by the parser when an error occurs. Use what() function to get human-readable error message. Use where() function to get a pointer to position within source text where error was detected.

this will at least let you discover what is causing the exception as well as the location. In addition your code does have an issue that could cause problems. This is taken from the rapidXML description of the parse function http://rapidxml.sourceforge.net/manual.html#classrapidxml_1_1xml__document_8338ce6042e7b04d5a42144fb446b69c_18338ce6042e7b04d5a42144fb446b69c

The bold text is emphasised by me

Parses zero-terminated XML string according to given flags. Passed string will be modified by the parser, unless rapidxml::parse_non_destructive flag is used. The string must persist for the lifetime of the document. In case of error, rapidxml::parse_error exception will be thrown.

But in your code

xml_document<> doc;
doc.parse<parse_full>(buffer);
//   doc.parse<0>(buffer);

delete [] buffer;

cout << "The first node is '" << doc.first_node()->name() << "'\n";

you are deleting the char buffer containing your string and afterwards calling functions on the doc object. This is a violation of the above documentation. I am not sure if this is the exact cause of your exception, but certainly deleting that buffer is going to cause problems. I would suggest using a try/catch block to catch the parse_error exception and then use the where() and what() functions to pinpoint the error. Also try moving the delete statement to the end of your code after you have completely finished calling functions on the doc object as that could also be causing problems.


Your parsed DOM object doc is based on your buffer at the memory so dont delete your buffer or delete just before quit

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜