Avatar of zc2
zc2
Flag for United States of America

asked on 

Setting the selection namespace fails

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( "https://api.authorize.net/xml/v1/request.api" ), 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

C++XML* com* atl

Avatar of undefined
Last Comment
zc2

8/22/2022 - Mon