如何解析多个特定节点的xml [英] how to parse xml for multiple specific node
本文介绍了如何解析多个特定节点的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
reboot.xml
< rebootexclusiondata >
< memid > 123 < / memid >
< siteid > 123 < / siteid >
< regid > 12345 < / regid >
< 由创建> tanvi < / createdby >
< 已更新 > null < / updatedby >
< 排除 > true < / exclusion >
< dcdtime > 2015-08-25 10:12:13 < / dcdtime >
< updcdtime < span class =code-keyword>> 2015-08-25 10:12:13 < / updcdtime >
< / rebootexclusiondata >
xmlparse.cpp:
void queryNodes()
{
HRESULT hr = S_OK;
IXMLDOMDocument * pXMLDom = NULL;
IXMLDOMNodeList * pNodes = NULL;
IXMLDOMNode * pNode = NULL;
IXMLDOMNode * pNode1 = NULL;
BSTR bstrQuery1 = NULL;
BSTR bstrQuery2 = NULL;
BSTR bstrNodeName = NULL;
BSTR bstrNodeValue = NULL;
VARIANT_BOOL varStatus;
VARIANT varFileName;
VariantInit(& varFileName);
//< siteid> 123< / siteid>
CHK_HR(CreateAndInitDOM(& pXMLDom));
CHK_HR(VariantFromString(Lstocks.xml,varFileName));
CHK_HR(pXMLDom-> load(varFileName,& varStatus));
if(varStatus!= VARIANT_TRUE)
{
CHK_HR(ReportParseError(pXMLDom,无法从stocks.xml加载DOM。));
}
//查询单个节点。
// bstrQuery1 = SysAllocString(L// rebootexclusiondata [2] / *);
bstrQuery1 = SysAllocString(L// rebootexclusiondata // exclusion //);
CHK_ALLOC(bstrQuery1);
CHK_HR(pXMLDom-> selectSingleNode(bstrQuery1,& pNode));
if(pNode)
{
printf(selectSingleNode的结果:\ n);
CHK_HR(pNode-> get_nodeName(& bstrNodeName));
CHK_HR(pNode-> get_xml(& bstrNodeValue));
SysFreeString(bstrNodeName);
printf(Node,<%S>:\ n,bstrNodeName);
printf(\ t%S \ n%S \ n,bstrNodeValue);
if(0 == wcscmp(bstrNodeValue,L< exclusion> true< / exclusion>))
{
printf(\ n exe exit \\\
\ n );
}
其他
{
printf(\ n更进一步!让我们重启系统\ n \\ n);
}
SysFreeString(bstrNodeValue);
SAFE_RELEASE(pNode);
}
现在在此代码中检查是否
< siteid> 123< / siteid>
与
< 排除 > true < < span class =code-leadattribute> / exclusion >
或不是
解决方案
如果你最好阅读文档,你会发现函数IXMLDOMNode::get_nextSibling 。
就在那里。通常,接口和类提供API,其中包含所有标准任务。这是一个高品质的功能。 ; - )
hey guys in my below code it checks for only 1 node whether that node is present or not. similarly i want to check the same for multiple specific node in same xml. so how do i do it.
reboot.xml
<rebootexclusiondata>
<memid>123</memid>
<siteid>123</siteid>
<regid>12345</regid>
<createdby>tanvi</createdby>
<updatedby>null</updatedby>
<exclusion>true</exclusion>
<dcdtime>2015-08-25 10:12:13</dcdtime>
<updcdtime>2015-08-25 10:12:13</updcdtime>
</rebootexclusiondata>
xmlparse.cpp:
void queryNodes() { HRESULT hr = S_OK; IXMLDOMDocument *pXMLDom = NULL; IXMLDOMNodeList *pNodes = NULL; IXMLDOMNode *pNode = NULL; IXMLDOMNode *pNode1 = NULL; BSTR bstrQuery1 = NULL; BSTR bstrQuery2 = NULL; BSTR bstrNodeName = NULL; BSTR bstrNodeValue = NULL; VARIANT_BOOL varStatus; VARIANT varFileName; VariantInit(&varFileName); //<siteid>123</siteid> CHK_HR(CreateAndInitDOM(&pXMLDom)); CHK_HR(VariantFromString(L"stocks.xml", varFileName)); CHK_HR(pXMLDom->load(varFileName, &varStatus)); if (varStatus != VARIANT_TRUE) { CHK_HR(ReportParseError(pXMLDom, "Failed to load DOM from stocks.xml.")); } // Query a single node. //bstrQuery1 = SysAllocString(L"//rebootexclusiondata[2]/*"); bstrQuery1 = SysAllocString(L"//rebootexclusiondata//exclusion//"); CHK_ALLOC(bstrQuery1); CHK_HR(pXMLDom->selectSingleNode(bstrQuery1, &pNode)); if (pNode) { printf("Result from selectSingleNode:\n"); CHK_HR(pNode->get_nodeName(&bstrNodeName)); CHK_HR(pNode->get_xml(&bstrNodeValue)); SysFreeString(bstrNodeName); printf("Node, <%S>:\n", bstrNodeName); printf("\t%S\n%S\n", bstrNodeValue); if (0 == wcscmp(bstrNodeValue, L"<exclusion>true</exclusion>")) { printf("\n exe exit \n\n"); } else { printf("\n go further! lets reboot the system\n\n"); } SysFreeString(bstrNodeValue); SAFE_RELEASE(pNode); }
now in this code it checks whether
<siteid>123</siteid>
is present along with
<exclusion>true</exclusion>
or not
解决方案
If you had better read the documentation you would had found the function IXMLDOMNode::get_nextSibling.
It's all there. Normally interfaces and classes provide an API in which all standard tasks are included. It is a quality feature. ;-)
这篇关于如何解析多个特定节点的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文