troubleshooting Question

Setting the selection namespace fails

Avatar of zc2
zc2Flag for United States of America asked on
C++XML* com* atl
2 Comments1 Solution56 ViewsLast Modified:
I am trying to use an XML HTTP API from C++ code. The returned XML has a declred default namespace.
If I use the responseXML property as the DOM tree to work with, the call setProperty( "SelectionNamespaces" ) does not set it.
If I call getProperty("SelectionNamespaces") right after it returns Empty.
Calling selectNodes() fails, with an error: "Reference to undeclared namespace prefix: 'a'".
But if I take the xml text from responseXML and load it to a standalone DOMDocument, I can set the NS and call selectNodes()

        // PostData()
        CComPtr<IXMLHTTPRequest> requestor;
        CoCreateInstance( CLSID_XMLHTTP60, NULL, CLSCTX_INPROC_SERVER, IID_IXMLHTTPRequest, (void**)&requestor );
        requestor->open( CComBSTR( "POST" ), CComBSTR( "" ), CComVariant( false ), 
                                    CComVariant(), CComVariant() );
        CComPtr<IDispatch> disp = request;
        requestor->send( CComVariant( disp ) );
        CComPtr<IDispatch> resp_xml_disp;
        requestor->get_responseXML( &resp_xml_disp );
        const CComPtr<IUnknown> pp = resp_xml_disp;
        CComPtr<IXMLDOMDocument> resp;
        resp = pp;
        // SetSelectionNamespaces()
        CComQIPtr<IXMLDOMDocument2> dom2( resp );
        dom2->setProperty( CComBSTR( "SelectionNamespaces" ), CComVariant( "xmlns:a='AnetApi/xml/v1/schema/AnetApiSchema.xsd'" ) );
        CComVariant nsv;
        dom2->getProperty(L"SelectionNamespaces", &nsv );  // returns Empty !!!

        // DocumentAsNode()
        CComPtr<IXMLDOMNode> dom_node;
        resp->QueryInterface( IID_IXMLDOMNode, (void**)&dom_node );
        // SelectNode()
        CComPtr<IXMLDOMNode> sel_node;
        dom_node->selectSingleNode( CComBSTR( "/a:ErrorResponse" ), &sel_node );
        CComBSTR xml_str;
        sel_node->get_xml( &xml_str );

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros