MsXml创建和解析XML示例

1 篇文章 0 订阅

一.MsXml创建XML文档示例

// XmlCreationDemo.cpp

#include <stdlib.h>
#include <stdio.h>

// 引入MSXML解析器
#import <msxml4.dll>
using namespace MSXML2;

class InitializeCom
{
public:
	InitializeCom()	{		CoInitialize(NULL); // Initializes the COM library	}
	~InitializeCom() {		CoUninitialize(); // Closes the COM library	}
}InitCom;

int main()
{
	char *szXmlFile = "D://china.xml"; // xml文件
	IXMLDOMDocumentPtr pDoc = NULL; // xml文档
	IXMLDOMProcessingInstructionPtr pProInstruction = NULL; // xml声明
	IXMLDOMCommentPtr pComment = NULL; // 注释
	IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素)
	IXMLDOMNodePtr pNode = NULL, pNode1 = NULL, pNode2 = NULL; // 节点
	IXMLDOMAttributePtr pAttrNode = NULL; // 属性

	HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40)); //
	if (FAILED(hr))
	{
		printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXml Parser库!");
		return EXIT_FAILURE;
	}
	
	// (1)创建xml文档声明(或insertBefore根节点)
	pProInstruction = pDoc->createProcessingInstruction((_bstr_t)(char*)"xml", (_bstr_t)(char*)"version=/"1.0/" encoding=/"utf-8/""); 
	pDoc->appendChild((IXMLDOMNode*)pProInstruction);

	// (2)创建根节点<China>
	pRootElement =  pDoc->createElement((_bstr_t)(char*)"China");	
	pDoc->PutRefdocumentElement(pRootElement); // pXMLDomDoc->documentElement = pRootElement;

	// (3)创建节点<China><Continent>	
	pComment = pDoc->createComment((_bstr_t)(char*)"所在的洲");
	pRootElement->appendChild((IXMLDOMNode*)pComment); // 注释
	
	pNode = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"Continent", (_bstr_t)(char*)"");
	pNode->Puttext((_bstr_t)(char*)"Asia"); // pNode->text = "Asia";
	pRootElement->appendChild(pNode); // 节点

	// (4)创建节点<China><Population>
	pComment = pDoc->createComment((_bstr_t)(char*)"人口数量");
	pRootElement->appendChild((IXMLDOMNode*)pComment); // 注释

	pElement = pDoc->createElement((_bstr_t)(char*)"Population");
	pAttrNode = pDoc->createAttribute((_bstr_t)(char*)"Units");
	pAttrNode->Puttext((_bstr_t)(char*)"Million Person");
	pElement->setAttributeNode(pAttrNode); // 统计单位
	pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
	pElement->Puttext((_bstr_t)(char*)"1,296");
	pRootElement->appendChild(pElement); // 节点

	// (5)创建节点<China><Municipality>
	pComment = pDoc->createComment((_bstr_t)(char*)"四个直辖市");
	pRootElement->appendChild((IXMLDOMNode*)pComment); // 注释

	pNode = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"Municipality", (_bstr_t)(char*)"");
	pRootElement->appendChild(pNode); // 节点

	// (6)创建节点<China><Municipality><TianJin>
	pNode1 = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"TianJin", (_bstr_t)(char*)"");
	
	//    创建节点<China><Municipality><TianJin><Area>
	pElement = pDoc->createElement((_bstr_t)(char*)"Area");
	pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 统计单位
	pElement->Puttext((_bstr_t)(char*)"12");
	pNode1->appendChild((IXMLDOMNode*)pElement); // 节点
	
	//    创建节点<China><Municipality><TianJin><Population>
	pElement = pDoc->createElement((_bstr_t)(char*)"Population");
	pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 统计单位
	pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
	pElement->Puttext((_bstr_t)(char*)"10.01");
	pNode1->appendChild((IXMLDOMNode*)pElement); // 节点

	pNode->appendChild(pNode1);
	// (7)创建节点<China><Municipality><BeiJing>并插入<TianJin>前
	pNode2 = pDoc->createNode((_variant_t)(long)NODE_ELEMENT, (_bstr_t)(char*)"BeiJing", (_bstr_t)(char*)"");

	//    创建节点<China><Municipality><BeiJing><Area>
	pElement = pDoc->createElement((_bstr_t)(char*)"Area");
	pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Thousand Square kilometers"); // 统计单位
	pElement->Puttext((_bstr_t)(char*)"17");
	pNode2->appendChild((IXMLDOMNode*)pElement); // 节点
	
	//    创建节点<China><Municipality><BeiJing><Population>
	pElement = pDoc->createElement((_bstr_t)(char*)"Population");
	pElement->setAttribute((_bstr_t)(char*)"Units", (_variant_t)(char*)"Million Person"); // 统计单位
	pElement->setAttribute((_bstr_t)(char*)"StatisticalYear", (_variant_t)(char*)"2000"); // 统计年份
	pElement->Puttext((_bstr_t)(char*)"13.82");
	pNode2->appendChild((IXMLDOMNode*)pElement); // 节点

	pNode->insertBefore(pNode2, (_variant_t)(IDispatch*)pNode1);
	//
	// (8)创建节点<China><Municipality><ShangHai>
	// (9)创建节点<China><Municipality><ChongQing>

	pDoc->save((_variant_t)szXmlFile);

	return EXIT_SUCCESS;
}

生成的china.xml文档内容:

<?xml version="1.0" encoding="utf-8"?>
<China>
<!--所在的洲-->
  <Continent>Asia</Continent>
  <!--人口数量-->
  <Population Units="Million Person" StatisticalYear="2000">1,296</Population>
  <!--四个直辖市-->
  <Municipality>
    <BeiJing>
      <Area Units="Thousand Square kilometers">17</Area>
      <Population Units="Million Person" StatisticalYear="2000">13.82</Population>
    </BeiJing>
    <TianJin>
      <Area Units="Thousand Square kilometers">12</Area>
      <Population Units="Million Person" StatisticalYear="2000">10.01</Population>
    </TianJin>
    <ShangHai>
      <Area Units="Thousand Square kilometers">6.4</Area>
      <Population Units="Million Person" StatisticalYear="2000">16.74</Population>
    </ShangHai>
    <ChongQing>
      <Area Units="Thousand Square kilometers">84</Area>
      <Population Units="Million Person" StatisticalYear="2000">30.90</Population>
    </ChongQing>
  </Municipality>
</China>

二.MsXml解析XML文档示例

// XmlParsingDemo.cpp

#include <stdlib.h>
#include <stdio.h>

// 引入MSXML解析器
#import <msxml4.dll>
using namespace MSXML2;

class InitializeCom
{
public:
	InitializeCom()	{		CoInitialize(NULL); // Initializes the COM library	}
	~InitializeCom() {		CoUninitialize(); // Closes the COM library	}
}InitCom;

int main()
{
	char *szXmlFile = "D://china.xml"; //上篇创建的xml文档
	IXMLDOMDocumentPtr pDoc = NULL; // xml文档
	IXMLDOMNodeListPtr pNodeList = NULL; // 节点链表
	IXMLDOMElementPtr pRootElement = NULL, pElement = NULL; // 根节点(元素)
	IXMLDOMNodePtr pNode = NULL, pNode1 = NULL; // 节点
	IXMLDOMNamedNodeMapPtr pAttrList = NULL; // 属性链表
	IXMLDOMAttributePtr pAttrNode = NULL; // 属性
	long lChilds, lAttr, i;

	HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument40));
	if (FAILED(hr))
	{
		printf("无法创建DOMDocument40对象,请检查是否安装并初始化了MsXml Parser库!");
		return EXIT_FAILURE;
	}

	VARIANT_BOOL bXmlLoad = pDoc->load((_variant_t)szXmlFile);
	if (!bXmlLoad) // 加载失败
	{
		printf("加载%s失败!/n", szXmlFile);
		return EXIT_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 ,(char*)pRootElement->GetnodeName(), (char*)pNode->GetnodeName());
		}
	}

	// (3)统计文档中所有的<Population>节点
	pNodeList = pDoc->getElementsByTagName((_bstr_t)(char*)"Population");
	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", (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", i, (char*)pNode->GetnodeName(), (char*)pAttrNode->GetnodeName(), (char*)pAttrNode->Gettext());
	}
	
	// (5)查找节点<Municipality>下的所有子节点
	// "//"表示在任意一层寻找Municipality;"//*"查找<Municipality></Municipality>中的所有子节点
	pNodeList = pDoc->selectNodes((_bstr_t)(char*)"//Municipality//*"); // 这里可将pDoc换成pRootElement
	while (pNode = pNodeList->nextNode())
	{
		printf("childs of [Municipality]: %s/n", (char*)pNode->GetnodeName());
	}

	// (6)查找节点<Municipality>下的一级子节点
	pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"Municipality");
	pNodeList = pNode->GetchildNodes();
	lChilds = pNodeList->Getlength();
	for (i = 0; i < lChilds; i++)
	{
		pNode1 = pNodeList->Getitem(i); // pNodeList->item[i]
		printf("child[%d] of [Municipality]: %s/n", i, (char*)pNode1->GetnodeName());
	}

	// (7)查询父、子、兄、弟节点
	pNode = pRootElement->selectSingleNode((_bstr_t)(char*)"//TianJin");
	pNode1 = pNode->GetparentNode(); // 父节点
	printf("[TianJin]的父节点为[%s]/n", (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", i, (char*)pNode1->GetnodeName());
	}

	pNode1 = pNode->GetpreviousSibling(); // 兄节点
	printf("[TianJin]的兄节点为[%s]/n", (char*)pNode1->GetnodeName());

	pNode1 = pNode->GetnextSibling(); // 弟节点
	printf("[TianJin]的弟节点为[%s]/n", (char*)pNode1->GetnodeName());

	return EXIT_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>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值