加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MsXml创建和解析XML示例

发布时间:2020-12-16 00:26:18 所属栏目:百科 来源:网络整理
导读:一.MsXml创建XML文档示例 [cpp] view plain copy print ? //XmlCreationDemo.cpp #includestdlib.h #includestdio.h //引入MSXML解析器 #importmsxml4.dll using namespace MSXML2; class InitializeCom { public : InitializeCom(){CoInitialize(NULL); //

一.MsXml创建XML文档示例

[cpp] view plain copy print ?
  1. //XmlCreationDemo.cpp
  2. #include<stdlib.h>
  3. #include<stdio.h>
  4. //引入MSXML解析器
  5. #import<msxml4.dll>
  6. usingnamespaceMSXML2;
  7. classInitializeCom
  8. {
  9. public:
  10. InitializeCom(){CoInitialize(NULL);//InitializestheCOMlibrary}
  11. ~InitializeCom(){CoUninitialize();//ClosestheCOMlibrary}
  12. }InitCom;
  13. intmain()
  14. {
  15. char*szXmlFile="D://china.xml";//xml文件
  16. IXMLDOMDocumentPtrpDoc=NULL;//xml文档
  17. IXMLDOMProcessingInstructionPtrpProInstruction=NULL;//xml声明
  18. IXMLDOMCommentPtrpComment=NULL;//注释
  19. IXMLDOMElementPtrpRootElement=NULL,pElement=NULL;//根节点(元素)
  20. IXMLDOMNodePtrpNode=NULL,pNode1=NULL,pNode2=NULL;//节点
  21. IXMLDOMAttributePtrpAttrNode=NULL;//属性
  22. HRESULThr=pDoc.CreateInstance(__uuidof(DOMDocument40));//
  23. if(FAILED(hr))
  24. {
  25. printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXmlParser库!");
  26. returnEXIT_FAILURE;
  27. }
  28. //(1)创建xml文档声明(或insertBefore根节点)
  29. pProInstruction=pDoc->createProcessingInstruction((_bstr_t)(char*)"xml",(_bstr_t)(char*)"version=/"1.0/"encoding=/"utf-8/"");
  30. pDoc->appendChild((IXMLDOMNode*)pProInstruction);
  31. //(2)创建根节点<China>
  32. pRootElement=pDoc->createElement((_bstr_t)(char*)"China");
  33. pDoc->PutRefdocumentElement(pRootElement);//pXMLDomDoc->documentElement=pRootElement;
  34. //(3)创建节点<China><Continent>
  35. pComment=pDoc->createComment((_bstr_t)(char*)"所在的洲");
  36. pRootElement->appendChild((IXMLDOMNode*)pComment);//注释
  37. pNode=pDoc->createNode((_variant_t)(long)NODE_ELEMENT,(_bstr_t)(char*)"Continent",87)">char*)"");
  38. pNode->Puttext((_bstr_t)(char*)"Asia");//pNode->text="Asia";
  39. pRootElement->appendChild(pNode);//节点
  40. //(4)创建节点<China><Population>
  41. pComment=pDoc->createComment((_bstr_t)(char*)"人口数量");
  42. pElement=pDoc->createElement((_bstr_t)(char*)"Population");
  43. pAttrNode=pDoc->createAttribute((_bstr_t)(char*)"Units");
  44. pAttrNode->Puttext((_bstr_t)(char*)"MillionPerson");
  45. pElement->setAttributeNode(pAttrNode);//统计单位
  46. pElement->setAttribute((_bstr_t)(char*)"StatisticalYear",(_variant_t)(char*)"2000");//统计年份
  47. pElement->Puttext((_bstr_t)(char*)"1,296");
  48. pRootElement->appendChild(pElement);//(5)创建节点<China><Municipality>
  49. pComment=pDoc->createComment((_bstr_t)(char*)"四个直辖市");
  50. char*)"Municipality",87)">char*)"");
  51. pRootElement->appendChild(pNode);//节点
  52. //(6)创建节点<China><Municipality><TianJin>
  53. pNode1=pDoc->createNode((_variant_t)(char*)"TianJin",87)">char*)"");
  54. //创建节点<China><Municipality><TianJin><Area>
  55. pElement=pDoc->createElement((_bstr_t)(char*)"Area");
  56. char*)"Units",87)">char*)"ThousandSquarekilometers");//统计单位
  57. pElement->Puttext((_bstr_t)(char*)"12");
  58. pNode1->appendChild((IXMLDOMNode*)pElement);//创建节点<China><Municipality><TianJin><Population>
  59. pElement=pDoc->createElement((_bstr_t)(char*)"Population");
  60. char*)"MillionPerson");//统计单位
  61. pElement->setAttribute((_bstr_t)(//统计年份
  62. pElement->Puttext((_bstr_t)(char*)"10.01");
  63. pNode1->appendChild((IXMLDOMNode*)pElement);
  64. pNode->appendChild(pNode1);
  65. //(7)创建节点<China><Municipality><BeiJing>并插入<TianJin>前
  66. pNode2=pDoc->createNode((_variant_t)(char*)"BeiJing",87)">char*)"");
  67. //创建节点<China><Municipality><BeiJing><Area>
  68. char*)"Area");
  69. pElement->setAttribute((_bstr_t)(//统计单位
  70. char*)"17");
  71. pNode2->appendChild((IXMLDOMNode*)pElement);//创建节点<China><Municipality><BeiJing><Population>
  72. char*)"Population");
  73. pElement->setAttribute((_bstr_t)(char*)"13.82");
  74. pNode2->appendChild((IXMLDOMNode*)pElement);pNode->insertBefore(pNode2,(_variant_t)(IDispatch*)pNode1);
  75. //
  76. //(8)创建节点<China><Municipality><ShangHai>
  77. //(9)创建节点<China><Municipality><ChongQing>
  78. pDoc->save((_variant_t)szXmlFile);
  79. returnEXIT_SUCCESS;
  80. }

生成的china.xml文档内容:

[xhtml]
  • <?xmlversion="1.0"encoding="utf-8"?>
  • <China>
  • <!--所在的洲-->
  • <Continent>Asia</Continent>
  • <!--人口数量-->
  • <PopulationUnits="MillionPerson"StatisticalYear="2000">1,296</Population>
  • <!--四个直辖市-->
  • <Municipality>
  • <BeiJing>
  • <AreaUnits="ThousandSquarekilometers">17</Area>
  • <PopulationUnits="MillionPerson"StatisticalYear="2000">13.82</Population>
  • </BeiJing>
  • <TianJin>
  • <AreaUnits="ThousandSquarekilometers">12</Area>
  • <PopulationUnits="MillionPerson"StatisticalYear="2000">10.01</Population>
  • </TianJin>
  • <ShangHai>
  • <AreaUnits="ThousandSquarekilometers">6.4</Area>
  • <PopulationUnits="MillionPerson"StatisticalYear="2000">16.74</Population>
  • </ShangHai>
  • <ChongQing>
  • <AreaUnits="ThousandSquarekilometers">84</Area>
  • <PopulationUnits="MillionPerson"StatisticalYear="2000">30.90</Population>
  • </ChongQing>
  • </Municipality>
  • </China>
  • 二.MsXml解析XML文档示例

    [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

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读