如何读取属性和元素值

发布于 2024-10-03 14:10:08 字数 1526 浏览 10 评论 0原文

我有以下 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);

        }

I have the following 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>

I need to read Attribute and Node values. I am able to get the number of child nodes but unsure how to get the value:

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);

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-10-10 14:10:08

通过 MSDN 进行快速搜索将为您节省大量时间:

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

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

A quick search through MSDN would have saved you a lot of time:

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

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

痴者 2024-10-10 14:10:08

在 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.

Looking in MSDN:

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

It seems that IXMLDOMNode has the text pointer, that points to the contents of the node:

text* : Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文