[cpp]
//XmlParsingDemo.cpp
char*szXmlFile="D://china.xml";//上篇创建的xml文档
//xml文档
IXMLDOMNodeListPtrpNodeList=NULL;//节点链表
IXMLDOMElementPtrpRootElement=NULL,0)">//根节点(元素)
IXMLDOMNodePtrpNode=NULL,pNode1=NULL;IXMLDOMNamedNodeMapPtrpAttrList=NULL;//属性链表
IXMLDOMAttributePtrpAttrNode=NULL;longlChilds,lAttr,i;
HRESULThr=pDoc.CreateInstance(__uuidof(DOMDocument40));
if(FAILED(hr))
{
printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXmlParser库!");
returnEXIT_FAILURE;
}
VARIANT_BOOLbXmlLoad=pDoc->load((_variant_t)szXmlFile);
if(!bXmlLoad)//加载失败
{
printf("加载%s失败!/n",szXmlFile);
returnEXIT_FAILURE;
//(1)根节点
pRootElement=pDoc->GetdocumentElement();
printf("root=%s/n",(char*)pRootElement->GetnodeName());//pRootElement->nodeName
//(2)根节点的一级子节点
pNodeList=pRootElement->GetchildNodes();//pRootElement->childNodes
lChilds=pNodeList->Getlength();//pNodeList->length
for(i=0;i<lChilds;i++)
{
pNode=pNodeList->Getitem(i);//pNodeList->item[i]
if(pNode->GetnodeType()!=NODE_COMMENT)//过滤注释节点
{
printf("child[%d]of[%s]:[%s]/n",i,87)">char*)pRootElement->GetnodeName(),(char*)pNode->GetnodeName());
}
//(3)统计文档中所有的<Population>节点
pNodeList=pDoc->getElementsByTagName((_bstr_t)(lChilds=pNodeList->Getlength();
printf("文档中[Population]共有%d个/n",lChilds);
//(4)根节点下的<Population>节点
pNode=pRootElement->selectSingleNode((_bstr_t)(char*)"Population");
//已知根节点为<China>时:pNode=pDoc->selectSingleNode((_bstr_t)(char*)"China//Population");
printf("根节点下的[Population]子节点值为%s/n",87)">char*)pNode->Gettext());
pAttrList=pNode->Getattributes();
lAttr=pAttrList->Getlength();
for(i=0;i<lAttr;i++)
{
pAttrNode=pAttrList->Getitem(i);
printf("Attr[%d]of[%s]:%s=%s/n",87)">char*)pNode->GetnodeName(),87)">char*)pAttrNode->GetnodeName(),87)">char*)pAttrNode->Gettext());
}
//(5)查找节点<Municipality>下的所有子节点
//"//"表示在任意一层寻找Municipality;"//*"查找<Municipality></Municipality>中的所有子节点
pNodeList=pDoc->selectNodes((_bstr_t)(char*)"//Municipality//*");//这里可将pDoc换成pRootElement
while(pNode=pNodeList->nextNode())
{
printf("childsof[Municipality]:%s/n",0)">//(6)查找节点<Municipality>下的一级子节点
char*)"Municipality");
pNodeList=pNode->GetchildNodes();
lChilds=pNodeList->Getlength();
for(i=0;i<lChilds;i++)
{
pNode1=pNodeList->Getitem(i);printf("child[%d]of[Municipality]:%s/n",87)">char*)pNode1->GetnodeName());
}
//(7)查询父、子、兄、弟节点
char*)"//TianJin");
pNode1=pNode->GetparentNode();//父节点
printf("[TianJin]的父节点为[%s]/n",87)">char*)pNode1->GetnodeName());
pNodeList=pNode->GetchildNodes();//子节点
lChilds=pNodeList->Getlength();
for(i=0;i<lChilds;i++)
{
pNode1=pNodeList->nextNode();
printf("child[%d]of[TianJin]:%s/n",87)">char*)pNode1->GetnodeName());
pNode1=pNode->GetpreviousSibling();//兄节点
printf("[TianJin]的兄节点为[%s]/n",85)">
pNode1=pNode->GetnextSibling();//弟节点
printf("[TianJin]的弟节点为[%s]/n",85)">returnEXIT_SUCCESS;
}
运行结果如下:
root = China
child[1] of <China>: <Continent>
child[3] of <China>: <Population>
child[5] of <China>: <Municipality>
文档中<Population>共有5个
根节点下的<Population>子节点值为1,296
Attr[0] of <Population>: Units = Million Person
Attr[1] of <Population>: StatisticalYear = 2000
childs of <Municipality>: BeiJing
childs of <Municipality>: Area
childs of <Municipality>: Population
childs of <Municipality>: TianJin
childs of <Municipality>: Area
childs of <Municipality>: Population
childs of <Municipality>: ShangHai
childs of <Municipality>: Area
childs of <Municipality>: Population
childs of <Municipality>: ChongQing
childs of <Municipality>: Area
childs of <Municipality>: Population
child[0] of <Municipality>: BeiJing
child[1] of <Municipality>: TianJin
child[2] of <Municipality>: ShangHai
child[3] of <Municipality>: ChongQing
<TianJin>的父节点为<Municipality>
child[0] of <TianJin>: Area
child[1] of <TianJin>: Population
<TianJin>的兄节点为<BeiJing>
<TianJin>的弟节点为<ShangHai>
http://blog.csdn.net/phunxm/article/details/5087102
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!