VC++ MSXML创建XML文件以及对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>
转载于:https://www.cnblogs.com/areliang/archive/2011/11/18/2254334.html
VC++ MSXML创建XML文件以及对XML文档解析相关推荐
- Python爬虫高级库之一的lxml库中,ET.parse()是一个非常重要的方法。它可以将任意XML或HTML格式的文档解析成一个ElementTree对象,...
Python爬虫高级库之一的lxml库中,ET.parse()是一个非常重要的方法.它可以将任意XML或HTML格式的文档解析成一个ElementTree对象,方便我们对结构化的数据进行处理和分析.在 ...
- linux右键没有创建新文件夹,将新建文档添加回Ubuntu 18.04中的右键菜单
当我最近转移到Ubuntu 18.04时,我注意到Nautilus的右键菜单中没有选项来创建一个空文本文件. 当然,我可以使用命令行快速创建新文档,甚至可以使用文本编辑器创建新文件,但这不是我想要的. ...
- Android -- 创建XML文件对象及其序列化, pull解析XML文件
1. 创建XML文件对象及其序列化 示例代码:(模拟以xml格式备份短信到SD卡) SmsInfo.java, bean对象 /*** 短信的业务bean* @author Administrator ...
- java生成xml文件head,生成XML文件 - Glucose的个人空间 - OSCHINA - 中文开源技术交流社区...
参照网上这篇博客所写,在此表示感谢 package com.qiux.demo; import java.io.FileOutputStream; import java.io.IOException ...
- vc 生成html,成功从VC++的XML注释生成静态html文档
GacUI的类库说明文档已经可以生成了!利用了之前的这篇博客描述的pdb信息抽取并和XML注释合并的技术,成功写了一系列工具来从这些信息里面生成静态html文档.现在的XML注释只写了1/3,所以生成 ...
- 本地html本地xml文件怎么打开,xml文件怎么打开
xml文件怎么打开 xml文件一般用用记事本或都是IE都可以打开. XML是可扩展标记语言(Extensible Markup Language,XML)缩写,用于标记电子文件使其具有结构性的标记语言 ...
- 02_Android写xml文件和读xml文件
新建Android项目 编写AndroidManifest.xml,使本Android项目具有单元测试功能和写外设的权限. <?xml version="1.0" en ...
- jdom解析xml文件_JDOM编辑XML文件示例
jdom解析xml文件 JDOM provides very neat way to manipulate XML files, using JDOM is very easy and the cod ...
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file”
背景:java读取xml文件,xml文件内容只有"<?xml version="1.0" encoding="UTF-8"?>" ...
最新文章
- Excel:python结合Excel使用技巧经验总结之(将python输出的等间隔列数据直接粘贴复制存到物理表格内等)图文教程之详细攻略
- 一个跨国银行的敏捷转型案例要点之全员培训
- jmeter-00 JMeter 运行过程
- 因为有人说的WZ132
- apache-storm分布式计算(drpc)开发心得
- Bootstrap-CSS-排版
- windows 上的应用性能测试
- 超越 YOLOv5 的目标检测开源项目又上新了
- 基于递归的前序二叉树遍历实现
- Java面向对象程序设计--与C++对比说明:系列2(类机制)
- 操作系统高响应比优先调度算法代码_进程调度
- Softmax Function
- CAN通讯程序C语言,基于单片机的CANBUS程序(C语言)
- android SharePreference缓存存储List<Bean>
- NPIO 简单读写 EXCEL 小李子
- speedoffice(Word)如何给文字添加下划线
- 电磁场与仿真软件(34)
- (四)Android中的TextView组件
- mysql linux 函数返回值_linux recv函数返回值分析
- SAP DELL磁带机( PowerVault 110T)的备份和恢复(图解)
热门文章
- 苏教版四下用计算机计算,苏教版四年级下册数学单元测试-4.用计算器计算 (含答案)...
- arp 不同网段 相同vlan_H3C交换机配置VLAN
- cm 怎么限制hue数据下载_0724-6.2.0-CM接管rpm方式安装的无CM的CDH集群-2
- charles 安装 ssl_前端开发如何使用抓包工具 charles
- 【Verilog HDL 训练】第 08 天(二进制、Johnson、环形计数器)
- Cordic算法——verilog实现
- Spring|AOP
- Nginx服务优化——性能与安全
- springboot EnableAutoConfiguration
- Qt编程之通过鼠标滚轮事件缩放QGraphicsView里面的Item