How can I access element attributes from IXMLDOMNode?

I am creating an XML DOM document in C ++. My problem is this: I am executing an XPATH request from an element in my document that I know will return another element. A call to elementPtr-> selectSingleNode returns IXMLDOMNode. How to access the attributes of this node?

Part of me wants to reduce Node to an element, but I could not get the cast to work.

I tried

MSXML2::IXMLDOMElementPtr pParentElement;
pParentNode->QueryInterface(__uuidof(MSXML2::IXMLDOMElement), 
                            (void**) &pParentElement);

This results in the following runtime error:

0x0057cc58 _com_error::`scalar deleting destructor'(unsigned int)

Another route I tried was just to use the nodes:

MSXML2::IXMLDOMNodePtr pParentNode = 
    pParameterElement->selectSingleNode("parent");
MSXML2::IXMLDOMNamedNodeMap* pParentAttributes;
pParentNode->get_attributes(&pParentAttributes);

MSXML2::IXMLDOMNodePtr pCategoryNameNode = 
    pParentAttributes->getNamedItem("Category");
VARIANT value;
pCategoryNameNode->get_nodeValue(&value);
CString categoryName = value;

This does not work with "parentNode-> get_attributes ()".

It seems like I'm missing something; The API should not be so difficult to use.

- edit -

, selectSingleNode , NULL. QueryInterface, get_attributes : P

, , , , , .

+3
2

, .

- , , .

MSXML2::IXMLDOMNodePtr pParentNode = pParameterElement->selectSingleNode("parent");
MSXML2::IXMLDOMElementPtr pParentElement( pParentNode );

Ptr , , MSXML . , ATL

CComPtr<IXMLDOMNode> node = ...;
CComQIPtr<IXMLDOMElement> elementNode( node );

if( elementNode ) { 
// it was an element!
} else { 
// it something else try again? 
}

...

CComPtr<IXMLDOMNamedNodeMap> attributes;
node->get_attributes( &attributes );
if( attributes ) {
  _bstr_t name( L"category" );
  attributes->getNamedItem(name);
}

COM, ++: (

+6

downcast IXMLDOMNode IXMLDOMElement? ++ , COM-: QueryInterface().


QueryInterface(), :

  • pParentNode ? , , , , .
  • QueryInterface() , : AddRef() , . , _com_ptr_t < > :

    MSXML2::IXMLDOMElementPtr pParentElement(pParentNode);
    

, , " ", , , AddRef()/Release().

, , pParentElement . , , , get_nodeType() pParentNode, , node. , XPath , .

+1

Source: https://habr.com/ru/post/1699541/


All Articles