如何读取属性和元素值
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 MSDN 进行快速搜索将为您节省大量时间:
请参阅: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:
See: http://msdn.microsoft.com/en-us/library/ms767592(v=VS.85).aspx
在 MSDN 中查找:
http://msdn.microsoft.com/en-us/library/ms761386
看来 IXMLDOMNode 有文本指针,指向节点的内容:
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: