如何读取属性和元素值

时间:2010-11-22 11:25:49

标签: c++ xml msxml msxml6

我有以下XML:

<?xml version="1.0" encoding="utf-8" ?>
<lastconnectedServers>
   <Server ip="172.24.18.240" domain="MyDomain">
        <SharedFolder name="MyShared" type="FTP"/>
        <SharedFolder name="Share1" type ="CIFS"/>
        <SharedFolder name="Share2" type ="both"/>
   </Server>
</lastconnectedServer>

我需要读取属性和节点值。我能够获得子节点的数量但不确定如何获取值:

CComPtr<IXMLDOMDocument> pIxmlDOC = NULL;
    CComPtr<IXMLDOMParseError> pIxmlDOM = NULL;
    CComPtr<IXMLDOMElement> pIxmlServerElement;
    CComPtr<IXMLDOMNode> pIxmlServerNode,pIxmlLastConnectedServerNode;
    CComPtr<IXMLDOMNodeList> pIXmlNodeChildList = NULL;


    IXMLDOMNode* pInsertedNode;

    BSTR bstrXML = NULL;
    VARIANT vXMLSource;
    VARIANT_BOOL bIsSuccess;
    HRESULT hr = CoInitialize(NULL);

    LONG lChildCount = 0;

    CHK_HR(CreateAndInitDOM(&pIxmlDOC));    
    VariantFromString(L"stocks.xml",vXMLSource);

    pIxmlDOC->load(vXMLSource,&bIsSuccess);

    pIxmlDOC->get_childNodes(&pIXmlNodeChildList);

        pIXmlNodeChildList->get_length(&lChildCount);

        for(int i = 0; i < lChildCount; i++)
        {
            CComPtr<IXMLDOMNamedNodeMap> pIXMLNodeMap;
            CComPtr<IXMLDOMNode> pIXMLChildNode;

            pIXmlNodeChildList->get_item(i,&pIXMLChildNode);
            pIXMLChildNode->get_attributes(&pIXMLNodeMap);

        }

2 个答案:

答案 0 :(得分:0)

通过MSDN快速搜索可以为您节省大量时间:

HRESULT getNamedItem(
    BSTR name,
    IXMLDOMNode **namedItem);

请参阅:http://msdn.microsoft.com/en-us/library/ms767592(v=VS.85).aspx

答案 1 :(得分:0)

查看MSDN:

http://msdn.microsoft.com/en-us/library/ms761386

似乎IXMLDOMNode有文本指针,指向节点的内容:

text* : Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write.