C++访问XML
生成树根
MSXML::IXMLDOMElementPtr pDocElement=pDoc->createElement("China");
pDoc->appendChild(pDocElement);
///添加节点
MSXML::IXMLDOMElementPtr pNewChildElement;
pNewChildElement=pDoc->createElement("Beijing");
pNewChildElement->Puttext("-11");
pDocElement->appendChild(pNewChildElement);
//显示树
count<<pDoc->xml<<endl;
//再添加节点
pNewChildElement=pDoc->createElement("Shanghai");
pNewChildElement->Puttext("9");
pNewChildElement->setAttribute("Weather",_variant_t("Cloudy"));
pDocElement->appendChild(pNewChildElement);
//把刚加入的节点删除
pDocElement->removeChild (pNewChildElement);
//保存文件
pDoc->save("test.XML");
//打开XML文件
MSXML::IXMLDOMDocumentPtr pDoc;
HRESULT hr =pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
if(!SUCCEEDED(hr))
{
cout << "无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!" << endl;
Exit();
}
pDoc->load("test.XML");
///定位并删除节点
MSXML::IXMLDOMElementPtr pDocElement=pDoc->GetdocumentElement();
MSXML::IXMLDOMElementPtr pElement=pDocElement->selectSingleNode("Shanghai");
pDocElement->removeChild(pElement);
//读取节点内容
MSXML::IXMLDOMNodePtr pRootNode=pDoc-> selectSingleNode( "China/Beijing ");
_bstr_t strRequestType=pRootNode-> Gettext();
cout<<"读取China/Beijing"<<strRequestType.operator char *()<<endl;
****************************************************************************
// testXML.cpp : 定义控制台应用程序的入口点。
#include "iostream"#include "msxml.h"
#include "atlbase.h"
#import "msxml.dll" //引入类型库
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int Exit();
void LoadFromString();
void CreateXML();
//XML文本模板
_bstr_t XMLTemple=" -11 9 ";
int main()
{
cout << " XML编程--示范程序 " << endl;
CoInitialize(NULL); //初始化COM 环境
cout << "------生成新的XML文本 ------" << endl;
CreateXML();
cout << "------读取已有XML文本 ------" << endl;
LoadFromString();
return Exit();
}
void CreateXML()
{
MSXML::IXMLDOMDocumentPtr pDoc;
HRESULT hr =pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
if(!SUCCEEDED(hr))
{
cout << "无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!" << endl;
Exit();
}
MSXML::IXMLDOMElementPtr pDocElement=pDoc->createElement("China");
pDoc->appendChild(pDocElement);
cout << "生成树根:\n" << pDoc->xml << endl;
MSXML::IXMLDOMElementPtr pNewChildElement;
pNewChildElement=pDoc->createElement("Beijing");
pNewChildElement->Puttext("-11");
pDocElement->appendChild(pNewChildElement);
cout << "添加节点:\n" << pDoc->xml << endl;
pNewChildElement=pDoc->createElement("Shanghai");
pNewChildElement->Puttext("9");
pNewChildElement->setAttribute("Weather",_variant_t("Cloudy"));
pDocElement->appendChild(pNewChildElement);
cout << "再添加节点:\n" << pDoc->xml << endl;
pDocElement->removeChild (pNewChildElement);
cout << "把刚加入的节点删除:\n" << pDoc->xml << endl;
pDoc->save("test.XML");
}
void LoadFromString()
{
MSXML::IXMLDOMDocumentPtr pDoc;
HRESULT hr =pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
if(!SUCCEEDED(hr))
{
cout << "无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!" << endl;
Exit();
}
pDoc->load("test.XML");
cout << "读取结果:\n" << pDoc->xml << endl;
MSXML::IXMLDOMNodePtr pRootNode=pDoc-> selectSingleNode( "China/Beijing ");
_bstr_t strRequestType=pRootNode-> Gettext();
cout<<"读取China/Beijing"<<strRequestType.operator char *()<<endl;
MSXML::IXMLDOMElementPtr pDocElement=pDoc->GetdocumentElement();
MSXML::IXMLDOMElementPtr pElement=pDocElement->selectSingleNode("Beijing");
pDocElement->removeChild(pElement);
cout << "定位删除Shanghai节点:\n" << pDoc->xml << endl;
cout << "保存结果 Save .....(模拟而已)" << endl;
cout << "\n 好了,就这么简单" << endl;
}
int Exit()
{
getchar();
return 1;
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/8265432
- 点赞
- 收藏
- 关注作者
评论(0)