使用 xerces for c++ 调用 getDocument 时出现分段错误

发布于 2024-12-10 20:16:47 字数 2383 浏览 0 评论 0原文

再会。我有 parser.ha Sample.cpp(作为父节点)和 main.cpp。我得到了 获取 getDocument 时出现分段错误。有人可以帮助我吗...这是我的代码:

parser.h

int Parser::Load(const char* xmlFile) {  
    try {  
        XMLPlatformUtils::Initialize();  
    }  
    catch (const XMLException& toCatch) {  
        char* message = XMLString::transcode(toCatch.getMessage());

        cout << "Error during initialization! :\n"  
             << message << "\n";  

        XMLString::release(&message);  
        return 1;  
    }

    XercesDOMParser* parser = new XercesDOMParser();  
    parser->setValidationScheme(XercesDOMParser::Val_Always);  
    parser->setDoNamespaces(true);  
    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();  
    parser->setErrorHandler(errHandler);  

    try {  
        parser->parse(xmlFile);  
    }
    catch (const XMLException& toCatch) {  
        char* message = XMLString::transcode(toCatch.getMessage());  

        cout << "Exception message is: \n"  
             << message << "\n";  

        XMLString::release(&message);  
        return -1;  
    }  
    catch (const DOMException& toCatch) {  
        char* message = XMLString::transcode(toCatch.msg);  

        cout << "Exception message is: \n"  
             << message << "\n";  

        XMLString::release(&message);  
        return -1;  
    }
    catch (...) {  
        cout << "Unexpected Exception \n";  
        return -1;  
    }  
}  

sample.cpp

void Sample::getXmlNodes(ParseXml* xmlParser){  
    map<std::string, std::string> attribList;  
    xmlParser->xmlDoc = xmlParser->parser->getDocument();  
    xmlParser->xmlNode = xmlParser->xmlDoc->getFirstChild();  
    attribList = xmlParser->getNodeAttribute(xmlParser->xmlNode);  
}  

ma​​in.cpp

#include "parser.h"  
#include "sample.cpp"

int main(int argc,char **argv){  
    const char* xmlString="sample.xml";  
    Parser* xmlObj;  
    Sample sampObj;

    xmlObj->Load(xmlString);  
    sampObj.getXmlNodes(xmlObj);  

    cout << "Done creating object..." << endl;  

    xmlObj->Cleanup();  
    return 0;  
}

我不知道我的代码出了什么问题代码 ...

Good day. I have parser.h a sample.cpp(as parent node) and main.cpp. Im getting
segmentation fault in getting getDocument. Can someone help me... Here's my code:

parser.h

int Parser::Load(const char* xmlFile) {  
    try {  
        XMLPlatformUtils::Initialize();  
    }  
    catch (const XMLException& toCatch) {  
        char* message = XMLString::transcode(toCatch.getMessage());

        cout << "Error during initialization! :\n"  
             << message << "\n";  

        XMLString::release(&message);  
        return 1;  
    }

    XercesDOMParser* parser = new XercesDOMParser();  
    parser->setValidationScheme(XercesDOMParser::Val_Always);  
    parser->setDoNamespaces(true);  
    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();  
    parser->setErrorHandler(errHandler);  

    try {  
        parser->parse(xmlFile);  
    }
    catch (const XMLException& toCatch) {  
        char* message = XMLString::transcode(toCatch.getMessage());  

        cout << "Exception message is: \n"  
             << message << "\n";  

        XMLString::release(&message);  
        return -1;  
    }  
    catch (const DOMException& toCatch) {  
        char* message = XMLString::transcode(toCatch.msg);  

        cout << "Exception message is: \n"  
             << message << "\n";  

        XMLString::release(&message);  
        return -1;  
    }
    catch (...) {  
        cout << "Unexpected Exception \n";  
        return -1;  
    }  
}  

sample.cpp

void Sample::getXmlNodes(ParseXml* xmlParser){  
    map<std::string, std::string> attribList;  
    xmlParser->xmlDoc = xmlParser->parser->getDocument();  
    xmlParser->xmlNode = xmlParser->xmlDoc->getFirstChild();  
    attribList = xmlParser->getNodeAttribute(xmlParser->xmlNode);  
}  

main.cpp

#include "parser.h"  
#include "sample.cpp"

int main(int argc,char **argv){  
    const char* xmlString="sample.xml";  
    Parser* xmlObj;  
    Sample sampObj;

    xmlObj->Load(xmlString);  
    sampObj.getXmlNodes(xmlObj);  

    cout << "Done creating object..." << endl;  

    xmlObj->Cleanup();  
    return 0;  
}

I don't know what's wrong in my code ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-12-17 20:16:47

您没有为 xmlObj objetc 分配内存:

Parser* xmlObj;
Sample sampObj;  
xmlObj->Load(xmlString);   // SIGSEV here, you have no object to invoke

必须替换为:

Parser xmlObj;  
Sample sampObj;  
xmlObj.Load(xmlString);
sampObj.getXmlNodes(&xmlObj);

You did not allocate memory for xmlObj objetc:

Parser* xmlObj;
Sample sampObj;  
xmlObj->Load(xmlString);   // SIGSEV here, you have no object to invoke

must be replaced by:

Parser xmlObj;  
Sample sampObj;  
xmlObj.Load(xmlString);
sampObj.getXmlNodes(&xmlObj);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文