我自己用Xercesc执行xPath的方法

My own method to execute xPath with Xercesc

本文关键字:方法 xPath 执行 Xercesc 我自己      更新时间:2023-10-16

我使用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;
}