我自己用 Xercesc 执行 xPath 的方法
My own method to execute xPath with Xercesc
我使用 Xercesc 库来解析 XML,但是这个库只能执行简单的 xPath。我已经为执行 xPath 编写了自己的方法,如下所示:
myProj/Parameters/Parameter[@Name="SomeName"]/Values/@DefaultValue
但是当我 运行 我的程序时我有 "myProj.exe has triggered breakpoint" 并且我在调试时看到“_CrtIsValidHeapPointer”方法定义。当我避免使用我的方法时 - 一切正常,所以我的代码一定有问题,但我不知道是什么。
string executeXPath(const char *A_xmlFile, string xPathh, shared_ptr<vector<string>> sectionSqlVector)
{
scoped_ptr<XercesDOMParser> parser(new XercesDOMParser());
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true); // optional
scoped_ptr<ErrorHandler> errHandler((ErrorHandler*) new HandlerBase());
parser->setErrorHandler(errHandler.get());
parser->parse(A_xmlFile);
const XMLCh* xPath = XMLString::transcode(xPathh.c_str());
int pos = XMLString::indexOf(xPath,'@');
int pos2 = XMLString::indexOf(xPath,('='));
XMLCh* attrName = new XMLCh;
XMLString::subString(attrName,xPath,pos+1,pos2);
pos = XMLString::indexOf(xPath,'@', pos+1);
XMLCh* attr2name = new XMLCh;
XMLString::subString(attr2name, xPath, pos+1, XMLString::stringLen(xPath));
pos = XMLString::indexOf(xPath,'"');
XMLCh* attribute = new XMLCh;
XMLString::subString(attribute, xPath, pos+1, XMLString::indexOf(xPath,'"',pos+1));
DOMNode* docRootNode;
DOMDocument* doc;
doc = parser->getDocument();
docRootNode = doc->getDocumentElement();
DOMElement* elementRoot = doc->getDocumentElement();
DOMNodeList* nodeList = elementRoot->getElementsByTagName(L"Parameter");
XMLSize_t d = nodeList->getLength();
string valuee = "";
for (XMLSize_t i = 0; i < d; i++)
{
DOMElement* tempElement = (DOMElement*)nodeList->item(i);
if (tempElement->hasAttribute(attrName))
{
const XMLCh* tempChar = tempElement->getAttribute(attrName);
if (XMLString::equals(attribute,tempChar))
{
DOMNodeList* tempList2 = tempElement->getElementsByTagName(L"Values");
DOMElement* tempElement2 = (DOMElement*)tempList2->item(0);
const XMLCh* defValue = tempElement2->getAttribute(attr2name);
valuee = XMLString::transcode(defValue);
tempElement2->release();
}
}
tempElement->release();
}
doc->release();
elementRoot->release();
docRootNode->release();
XMLString::release(&attr2name);
XMLString::release(&attrName);
XMLString::release(&attribute);
return valuee;
}
我找到了适合我需要的方法。
string executeXPath(xercesc::DOMDocument* doc, string xPathh)
{
const XMLCh* xPath = XMLString::transcode(xPathh.c_str());
int pos = XMLString::indexOf(xPath,'@');
int pos2 = XMLString::indexOf(xPath,('='));
XMLCh* attrName = (XMLCh*)malloc(sizeof(wchar_t)*(pos2-pos));
XMLString::subString(attrName,xPath,pos+1,pos2);
pos = XMLString::indexOf(xPath,'@', pos+1);
XMLCh* attrName2 = (XMLCh*)malloc(sizeof(wchar_t)*(XMLString::stringLen(xPath)-pos));
XMLString::subString(attrName2, xPath, pos+1, XMLString::stringLen(xPath));
pos = XMLString::indexOf(xPath,'"');
XMLCh* attribute = (XMLCh*)malloc(sizeof(wchar_t)*(XMLString::indexOf(xPath,'"',pos+1)-pos));
XMLString::subString(attribute, xPath, pos+1, XMLString::indexOf(xPath,'"',pos+1));
DOMElement* elementRoot = doc->getDocumentElement();
DOMNodeList* nodeList = elementRoot->getElementsByTagName(L"Parameter");
XMLSize_t d = nodeList->getLength();
string valuee = "";
for (XMLSize_t i = 0; i < d; i++)
{
DOMElement* tempElement = (DOMElement*)nodeList->item(i);
if (tempElement->hasAttribute(attrName))
{
const XMLCh* tempChar = tempElement->getAttribute(attrName);
if (XMLString::equals(attribute,tempChar))
{
DOMNodeList* tempList2 = tempElement->getElementsByTagName(L"Values");
DOMElement* tempElement2 = (DOMElement*)tempList2->item(0);
const XMLCh* defValue = tempElement2->getAttribute(attrName2);
valuee = XMLString::transcode(defValue);
break;
}
}
}
delete attrName2;
delete attrName;
delete attribute;
return valuee;
}
我使用 Xercesc 库来解析 XML,但是这个库只能执行简单的 xPath。我已经为执行 xPath 编写了自己的方法,如下所示:
myProj/Parameters/Parameter[@Name="SomeName"]/Values/@DefaultValue
但是当我 运行 我的程序时我有 "myProj.exe has triggered breakpoint" 并且我在调试时看到“_CrtIsValidHeapPointer”方法定义。当我避免使用我的方法时 - 一切正常,所以我的代码一定有问题,但我不知道是什么。
string executeXPath(const char *A_xmlFile, string xPathh, shared_ptr<vector<string>> sectionSqlVector)
{
scoped_ptr<XercesDOMParser> parser(new XercesDOMParser());
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true); // optional
scoped_ptr<ErrorHandler> errHandler((ErrorHandler*) new HandlerBase());
parser->setErrorHandler(errHandler.get());
parser->parse(A_xmlFile);
const XMLCh* xPath = XMLString::transcode(xPathh.c_str());
int pos = XMLString::indexOf(xPath,'@');
int pos2 = XMLString::indexOf(xPath,('='));
XMLCh* attrName = new XMLCh;
XMLString::subString(attrName,xPath,pos+1,pos2);
pos = XMLString::indexOf(xPath,'@', pos+1);
XMLCh* attr2name = new XMLCh;
XMLString::subString(attr2name, xPath, pos+1, XMLString::stringLen(xPath));
pos = XMLString::indexOf(xPath,'"');
XMLCh* attribute = new XMLCh;
XMLString::subString(attribute, xPath, pos+1, XMLString::indexOf(xPath,'"',pos+1));
DOMNode* docRootNode;
DOMDocument* doc;
doc = parser->getDocument();
docRootNode = doc->getDocumentElement();
DOMElement* elementRoot = doc->getDocumentElement();
DOMNodeList* nodeList = elementRoot->getElementsByTagName(L"Parameter");
XMLSize_t d = nodeList->getLength();
string valuee = "";
for (XMLSize_t i = 0; i < d; i++)
{
DOMElement* tempElement = (DOMElement*)nodeList->item(i);
if (tempElement->hasAttribute(attrName))
{
const XMLCh* tempChar = tempElement->getAttribute(attrName);
if (XMLString::equals(attribute,tempChar))
{
DOMNodeList* tempList2 = tempElement->getElementsByTagName(L"Values");
DOMElement* tempElement2 = (DOMElement*)tempList2->item(0);
const XMLCh* defValue = tempElement2->getAttribute(attr2name);
valuee = XMLString::transcode(defValue);
tempElement2->release();
}
}
tempElement->release();
}
doc->release();
elementRoot->release();
docRootNode->release();
XMLString::release(&attr2name);
XMLString::release(&attrName);
XMLString::release(&attribute);
return valuee;
}
我找到了适合我需要的方法。
string executeXPath(xercesc::DOMDocument* doc, string xPathh)
{
const XMLCh* xPath = XMLString::transcode(xPathh.c_str());
int pos = XMLString::indexOf(xPath,'@');
int pos2 = XMLString::indexOf(xPath,('='));
XMLCh* attrName = (XMLCh*)malloc(sizeof(wchar_t)*(pos2-pos));
XMLString::subString(attrName,xPath,pos+1,pos2);
pos = XMLString::indexOf(xPath,'@', pos+1);
XMLCh* attrName2 = (XMLCh*)malloc(sizeof(wchar_t)*(XMLString::stringLen(xPath)-pos));
XMLString::subString(attrName2, xPath, pos+1, XMLString::stringLen(xPath));
pos = XMLString::indexOf(xPath,'"');
XMLCh* attribute = (XMLCh*)malloc(sizeof(wchar_t)*(XMLString::indexOf(xPath,'"',pos+1)-pos));
XMLString::subString(attribute, xPath, pos+1, XMLString::indexOf(xPath,'"',pos+1));
DOMElement* elementRoot = doc->getDocumentElement();
DOMNodeList* nodeList = elementRoot->getElementsByTagName(L"Parameter");
XMLSize_t d = nodeList->getLength();
string valuee = "";
for (XMLSize_t i = 0; i < d; i++)
{
DOMElement* tempElement = (DOMElement*)nodeList->item(i);
if (tempElement->hasAttribute(attrName))
{
const XMLCh* tempChar = tempElement->getAttribute(attrName);
if (XMLString::equals(attribute,tempChar))
{
DOMNodeList* tempList2 = tempElement->getElementsByTagName(L"Values");
DOMElement* tempElement2 = (DOMElement*)tempList2->item(0);
const XMLCh* defValue = tempElement2->getAttribute(attrName2);
valuee = XMLString::transcode(defValue);
break;
}
}
}
delete attrName2;
delete attrName;
delete attribute;
return valuee;
}