MSDN XPath

  • IXMLDOMDocument *pXMLDoc = NULL
        IXMLDOMNode *pNode = NULL
        BSTR     bstr = NULL;  
        VARIANT_BOOL status; 
        VARIANT var; 
        HRESULT hr; 
        int i = 0
     
        CoInitialize(NULL); 
        VariantInit(&var); 
     
        hr = CoCreateInstance(CLSID_DOMDocument, NULL,  
            CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER 
            , IID_IXMLDOMDocument, (LPVOID*)&pXMLDoc); 
        if (!pXMLDoc || pXMLDoc==NULL) { 
            iItems = -1; 
            // DOM creation failed! 
        } 
     
        VariantClear(&var); 
        var = bstr2var(_bstr_t("d:\book.xml")); 
        hr = pXMLDoc->load(var, &status); 
        if (status!=VARIANT_TRUE) { 
            iItems = -2; 
            // unable to load file! 
        } 
     
        pXMLDoc->selectSingleNode(L"catalog/book[last()]/author", &pNode);       
     
        if (pNode!=NULL) { 
            pNode->get_text(&bstr); 
            CString u; 
            u.Format("%s", (LPCTSTR)(_bstr_t)bstr); 
            AfxMessageBox(u); 
        } else { 
            AfxMessageBox("pNode = NULL!"); 
        } 

    The result of above expression is pNode = NULL.

    I use VC++ 6.0 and MSXML2 (just curious, I also tried to use VC 2005 and MSXML6 with similar code as above, but the result is the same)

    Did I miss something?

    Thanks!

    -nb: I test the expression using BIT-101 (http://www.bit-101.com/xpath/) and works fine.


    Tuesday, November 11, 2008 1:40 PM
    Avatar of Houari
    15 Points

All replies

  • To use XPath you have to set the property "SelectionLanguage" to "XPath". 

    pXMLDoc->setProperty("SelectionLanguage", "XPath");

    To be able to do this, you have to use the IXMLDOMDocument2 interface instead of IXMLDOMDocument.

    Read more about How To Use XPath Queries in MSXML.
原文地址:https://www.cnblogs.com/eaglezzb/p/4176459.html