使用 xerces for c++ 调用 getDocument 时出现分段错误
再会。我有 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);
}
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;
}
我不知道我的代码出了什么问题代码 ...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有为
xmlObj
objetc 分配内存:必须替换为:
You did not allocate memory for
xmlObj
objetc:must be replaced by: