再会。我有 parser.ha sample.cpp(作为父节点)和 main.cpp。我在获取 getDocument 时遇到分段错误。有人可以帮助我...这是我的代码:
解析器.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;
}
}
样本.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);
}
主文件
#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;
}
我不知道我的代码有什么问题...