MsXml创建和解析XML示例

一.MsXml创建XML文档示例

  1. // XmlCreationDemo.cpp
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. // 引入MSXML解析器
  5. #import <msxml4.dll>
  6. using namespace MSXML2;
  7. class InitializeCom
  8. {
  9. public:
  10. InitializeCom() { CoInitialize(NULL); // Initializes the COM library }
  11. ~InitializeCom() { CoUninitialize(); // Closes the COM library }
  12. }InitCom;
  13. int main()
  14. {
  15. char *szXmlFile = "D://china.xml"; // xml文件
  16. IXMLDOMDocumentPtr pDoc = NULL; // xml文档
  17. IXMLDOMProcessingInstructionPtr pProInstruction = NULL; // xml声明
  18. IXMLDOMCommentPtr pComment = NULL; // 注释
  19. IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素)
  20. IXMLDOMNodePtr pNode = NULL, pNode1 = NULL, pNode2 = NULL; // 节点
  21. IXMLDOMAttributePtr pAttrNode = NULL; // 属性
  22. HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40)); //
  23. if (FAILED(hr))
  24. {
  25. printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXml Parser库!");
  26. return EXIT_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", (_bstr_t)(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. pRootElement->appendChild((IXMLDOMNode*)pComment); // 注释
  43. pElement = pDoc->createElement((_bstr_t)(char*)"Population");
  44. pAttrNode = pDoc->createAttribute((_bstr_t)(char*)"Units");
  45. pAttrNode->Puttext((_bstr_t)(char*)"Million Person");
  46. pElement->setAttributeNode(pAttrNode); // 统计单位
  47. pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
  48. pElement->Puttext((_bstr_t)(char*)"1,296");
  49. pRootElement->appendChild(pElement); // 节点
  50. // (5)创建节点<China><Municipality>
  51. pComment = pDoc->createComment((_bstr_t)(char*)"四个直辖市");
  52. pRootElement->appendChild((IXMLDOMNode*)pComment); // 注释
  53. pNode = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"Municipality", (_bstr_t)(char*)"");
  54. pRootElement->appendChild(pNode); // 节点
  55. // (6)创建节点<China><Municipality><TianJin>
  56. pNode1 = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"TianJin", (_bstr_t)(char*)"");
  57. // 创建节点<China><Municipality><TianJin><Area>
  58. pElement = pDoc->createElement((_bstr_t)(char*)"Area");
  59. pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 统计单位
  60. pElement->Puttext((_bstr_t)(char*)"12");
  61. pNode1->appendChild((IXMLDOMNode*)pElement); // 节点
  62. // 创建节点<China><Municipality><TianJin><Population>
  63. pElement = pDoc->createElement((_bstr_t)(char*)"Population");
  64. pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 统计单位
  65. pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
  66. pElement->Puttext((_bstr_t)(char*)"10.01");
  67. pNode1->appendChild((IXMLDOMNode*)pElement); // 节点
  68. pNode->appendChild(pNode1);
  69. // (7)创建节点<China><Municipality><BeiJing>并插入<TianJin>前
  70. pNode2 = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"BeiJing", (_bstr_t)(char*)"");
  71. // 创建节点<China><Municipality><BeiJing><Area>
  72. pElement = pDoc->createElement((_bstr_t)(char*)"Area");
  73. pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 统计单位
  74. pElement->Puttext((_bstr_t)(char*)"17");
  75. pNode2->appendChild((IXMLDOMNode*)pElement); // 节点
  76. // 创建节点<China><Municipality><BeiJing><Population>
  77. pElement = pDoc->createElement((_bstr_t)(char*)"Population");
  78. pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 统计单位
  79. pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
  80. pElement->Puttext((_bstr_t)(char*)"13.82");
  81. pNode2->appendChild((IXMLDOMNode*)pElement); // 节点
  82. pNode->insertBefore(pNode2, (_variant_t)(IDispatch*)pNode1);
  83. //
  84. // (8)创建节点<China><Municipality><ShangHai>
  85. // (9)创建节点<China><Municipality><ChongQing>
  86. pDoc->save((_variant_t)szXmlFile);
  87. return EXIT_SUCCESS;
  88. }

生成的china.xml文档内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <China>
  3. <!--所在的洲-->
  4. <Continent>Asia</Continent>
  5. <!--人口数量-->
  6. <Population Units="Million Person" StatisticalYear="2000">1,296</Population>
  7. <!--四个直辖市-->
  8. <Municipality>
  9. <BeiJing>
  10. <Area Units="Thousand Square kilometers">17</Area>
  11. <Population Units="Million Person" StatisticalYear="2000">13.82</Population>
  12. </BeiJing>
  13. <TianJin>
  14. <Area Units="Thousand Square kilometers">12</Area>
  15. <Population Units="Million Person" StatisticalYear="2000">10.01</Population>
  16. </TianJin>
  17. <ShangHai>
  18. <Area Units="Thousand Square kilometers">6.4</Area>
  19. <Population Units="Million Person" StatisticalYear="2000">16.74</Population>
  20. </ShangHai>
  21. <ChongQing>
  22. <Area Units="Thousand Square kilometers">84</Area>
  23. <Population Units="Million Person" StatisticalYear="2000">30.90</Population>
  24. </ChongQing>
  25. </Municipality>
  26. </China>

二.MsXml解析XML文档示例

  1. // XmlParsingDemo.cpp
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. // 引入MSXML解析器
  5. #import <msxml4.dll>
  6. using namespace MSXML2;
  7. class InitializeCom
  8. {
  9. public:
  10. InitializeCom() { CoInitialize(NULL); // Initializes the COM library }
  11. ~InitializeCom() { CoUninitialize(); // Closes the COM library }
  12. }InitCom;
  13. int main()
  14. {
  15. char *szXmlFile = "D://china.xml"; //上篇创建的xml文档
  16. IXMLDOMDocumentPtr pDoc = NULL; // xml文档
  17. IXMLDOMNodeListPtr pNodeList = NULL; // 节点链表
  18. IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素)
  19. IXMLDOMNodePtr pNode = NULL, pNode1 = NULL; // 节点
  20. IXMLDOMNamedNodeMapPtr pAttrList = NULL; // 属性链表
  21. IXMLDOMAttributePtr pAttrNode = NULL; // 属性
  22. long lChilds, lAttr, i;
  23. HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40));
  24. if (FAILED(hr))
  25. {
  26. printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXml Parser库!");
  27. return EXIT_FAILURE;
  28. }
  29. VARIANT_BOOL bXmlLoad = pDoc->load((_variant_t)szXmlFile);
  30. if (!bXmlLoad) // 加载失败
  31. {
  32. printf("加载%s失败!/n", szXmlFile);
  33. return EXIT_FAILURE;
  34. }
  35. // (1)根节点
  36. pRootElement = pDoc->GetdocumentElement();
  37. printf("root = %s/n", (char*)pRootElement->GetnodeName()); // pRootElement->nodeName
  38. // (2)根节点的一级子节点
  39. pNodeList = pRootElement->GetchildNodes(); // pRootElement->childNodes
  40. lChilds = pNodeList->Getlength(); // pNodeList->length
  41. for (i = 0; i < lChilds; i++)
  42. {
  43. pNode = pNodeList->Getitem(i); // pNodeList->item[i]
  44. if (pNode->GetnodeType() != NODE_COMMENT) // 过滤注释节点
  45. {
  46. printf("child[%d] of [%s]: [%s]/n", i ,(char*)pRootElement->GetnodeName(), (char*)pNode->GetnodeName());
  47. }
  48. }
  49. // (3)统计文档中所有的<Population>节点
  50. pNodeList = pDoc->getElementsByTagName((_bstr_t)(char*)"Population");
  51. lChilds = pNodeList->Getlength();
  52. printf("文档中[Population]共有%d个/n", lChilds);
  53. // (4)根节点下的<Population>节点
  54. pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Population");
  55. // 已知根节点为<China>时:pNode = pDoc->selectSingleNode((_bstr_t)(char*)"China//Population");
  56. printf("根节点下的[Population]子节点值为%s/n", (char*)pNode->Gettext());
  57. pAttrList = pNode->Getattributes();
  58. lAttr = pAttrList->Getlength();
  59. for (i = 0; i < lAttr; i++)
  60. {
  61. pAttrNode = pAttrList->Getitem(i);
  62. printf("Attr[%d] of [%s]: %s = %s/n", i, (char*)pNode->GetnodeName(), (char*)pAttrNode->GetnodeName(), (char*)pAttrNode->Gettext());
  63. }
  64. // (5)查找节点<Municipality>下的所有子节点
  65. // "//"表示在任意一层寻找Municipality;"//*"查找<Municipality></Municipality>中的所有子节点
  66. pNodeList = pDoc->selectNodes((_bstr_t)(char*)"//Municipality//*"); // 这里可将pDoc换成pRootElement
  67. while (pNode = pNodeList->nextNode())
  68. {
  69. printf("childs of [Municipality]: %s/n", (char*)pNode->GetnodeName());
  70. }
  71. // (6)查找节点<Municipality>下的一级子节点
  72. pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Municipality");
  73. pNodeList = pNode->GetchildNodes();
  74. lChilds = pNodeList->Getlength();
  75. for (i = 0; i < lChilds; i++)
  76. {
  77. pNode1 = pNodeList->Getitem(i); // pNodeList->item[i]
  78. printf("child[%d] of [Municipality]: %s/n", i, (char*)pNode1->GetnodeName());
  79. }
  80. // (7)查询父、子、兄、弟节点
  81. pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"//TianJin");
  82. pNode1 = pNode->GetparentNode(); // 父节点
  83. printf("[TianJin]的父节点为[%s]/n", (char*)pNode1->GetnodeName());
  84. pNodeList = pNode->GetchildNodes(); // 子节点
  85. lChilds = pNodeList->Getlength();
  86. for (i = 0; i < lChilds; i++)
  87. {
  88. pNode1 = pNodeList->nextNode();
  89. printf("child[%d] of [TianJin]: %s/n", i, (char*)pNode1->GetnodeName());
  90. }
  91. pNode1 = pNode->GetpreviousSibling(); // 兄节点
  92. printf("[TianJin]的兄节点为[%s]/n", (char*)pNode1->GetnodeName());
  93. pNode1 = pNode->GetnextSibling(); // 弟节点
  94. printf("[TianJin]的弟节点为[%s]/n", (char*)pNode1->GetnodeName());
  95. return EXIT_SUCCESS;
  96. }

运行结果如下:

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值