代码之家  ›  专栏  ›  技术社区  ›  Adek

我自己使用Xercesc执行xPath的方法

  •  1
  • Adek  · 技术社区  · 9 年前

    我使用Xercesc库解析XML,但这个库只能执行简单的xPath。我已经编写了自己的执行xPath的方法,如下所示:

    myProj/参数/参数[@Name=“SomeName”]/值/@DefaultValue

    但当我运行程序时,我看到“myProj.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;
    
        }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Adek    9 年前

    我已经找到了一种适合我需要的方法。

    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;
    }