我自己用Xercesc

时间:2015-08-31 10:20:37

标签: c++ xml xpath xerces

我使用Xercesc库来解析XML,但是这个库只能执行简单的xPath。我为这个执行xPath写了我自己的methot:

  

的Myproj /参数/参数[@Name =" SomeName"] /值/ @默认值

但是当我运行我的程序时,我已经" 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 个答案:

答案 0 :(得分:0)

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

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