我自己用Xercesc执行xPath的方法
My own method to execute xPath with Xercesc
我使用Xercesc库来解析XML,但是这个库只能执行简单的xPath。我自己编写了执行xPath的方法,如下所示:
myProj/参数/参数[@ name = " SomeName "]/价值/@DefaultValue
但是当我运行我的程序时,我有"myproject .exe已触发断点",我在调试时看到"_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;
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 我自己用Xercesc执行xPath的方法