最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Vc 读写xml文件二个实例
时间:2022-06-25 04:58:59 编辑:袖梨 来源:一聚教程网
vc 读写xml文件二个实例
13
保护
c++代码为:
coinitialize(null);
msxml2::ixmldomdocumentptr pdoc;
msxml2::ixmldomelementptr xmlroot;hresult hr = pdoc.createinstance(__uuidof(msxml2::domdocument30));
if(!succeeded(hr))
{
messagebox("无法创建domdocument对象,请检查是否安装了ms xmlparser 运行库!");
}pdoc->load("f:test.xml");
//找到xml的跟节点
xmlroot=(msxml2::ixmldomelementptr)pdoc->selectsinglenode("//root");msxml2::ixmldomelementptr childnode,childnode1;
//添加一个新的“device”
childnode=pdoc->createelement((_bstr_t)"device");
childnode->setattribute("id",(const char*)"10042");
childnode->setattribute("name",(const char*)"设备2");
xmlroot->appendchild(childnode);//type节点
childnode1=pdoc->createelement((_bstr_t)"type");
childnode1->puttext((_bstr_t)"14");
childnode->appendchild(childnode1);childnode1=pdoc->createelement((_bstr_t)"typename");
childnode1->puttext((_bstr_t)"保护2");
childnode->appendchild(childnode1);pdoc->save("f:test.xml");
coinitialize(null);
下面看个简单实现
建立一个基于dialog的mfc工程,工程名为operatexml,然后在对话框上添加两个按钮("create"和"getdata"和一个listcontrol
控件(用来显示数据),首先要导入库文件
#import "msxml3.dll"
using namespace msxml2;
主要用来实现xml文件的相关操作,
双击"create"按钮进入代码编写,代码如下:
void coperatexml::onbtncreate()
{
// todo: add your control notification handler code here
::coinitialize(null); //初始化com
msxml2::ixmldomdocumentptr pdoc;
msxml2::ixmldomelementptr xmlroot;
hresult hr = pdoc.createinstance(__uuidof(msxml2::domdocument30));
if(!succeeded(hr))
{
messagebox("error");
return;
}
pdoc->raw_createelement((_bstr_t)(char*)"china", &xmlroot);
pdoc->raw_appendchild(xmlroot, null);msxml2::ixmldomelementptr childnode;
pdoc->raw_createelement((_bstr_t)(char*)"city", &childnode);
childnode->puttext("shanghai");
childnode->setattribute("population", "7000");
childnode->setattribute("area", "2000");
xmlroot->appendchild(childnode);pdoc->raw_createelement((_bstr_t)(char*)"city", &childnode);
childnode->puttext("beijing");
childnode->setattribute("population", "39999");
childnode->setattribute("area", "3322");
xmlroot->appendchild(childnode);pdoc->save("d:test.xml");
}双击"getdata"按钮进入代码编写,代码如下:
void coperatexml::onbtnget()
{
// todo: add your control notification handler code here
m_list.deleteallitems();
msxml2::ixmldomdocumentptr pdoc;
hresult hr = pdoc.createinstance(__uuidof(msxml2::domdocument30));
if(!succeeded(hr))
{
messagebox("error!");
return;
}
pdoc->load("d:test.xml");
msxml2::ixmldomelementptr childnode;
childnode = (msxml2::ixmldomelementptr)(pdoc->selectsinglenode("//city"));msxml2::domnodetype nodetype;
childnode->get_nodetype(&nodetype);msxml2::ixmldomnamednodemapptr pattrs = null;
msxml2::ixmldomnodelistptr nodelist;
msxml2::ixmldomnodeptr pattritem;
childnode->get_attributes(&pattrs);
childnode->get_childnodes(&nodelist);
long ncount, icount;
pattrs->get_length(&ncount); //获取节点属性个数
nodelist->get_length(&icount); //获取节点个数
for(int i = 0; i < icount; i++) //根据需要可以添加数据到listcontrol中
{
for(int j = 0; j < ncount; j++)
{
pattrs->get_item(j, &pattritem);
cstring strattrname = (char*)(_bstr_t)pattritem->nodename;
cstring strattrvalue = (char*)(_bstr_t)pattritem->nodetypedvalue;
m_list.insertitem(i, strattrname);
m_list.setitemtext(i, 1, strattrvalue);
}
}}
为listcontrol控件绑定一个操作变量
clistctrl m_list;
在oninitdialog中初始化,代码如下:
bool coperatexml::oninitdialog()
{
// todo: add extra initialization here
m_list.insertcolumn(0, "人口");
m_list.insertcolumn(1, "num");
m_list.insertcolumn(1, "面积");
m_list.insertcolumn(1, "num");
crect rect;
m_list.getclientrect(&rect);
int width = (rect.right - rect.left) / 4;
m_list.setcolumnwidth(0, width);
m_list.setcolumnwidth(1, width);
m_list.setcolumnwidth(2, width);
m_list.setcolumnwidth(3, width);
}
相关文章
- 《尼尔:机械纪元》武器黑之倨傲属性及特殊能力介绍 11-15
- 《尼尔:机械纪元》机械生命体的枪获得方法介绍 11-15
- 《尼尔:机械纪元》武器机械生命体的枪属性及特殊能力介绍 11-15
- 《尼尔:机械纪元》天使之圣翼获得方法介绍 11-15
- 《尼尔:机械纪元》武器天使之圣翼属性及特殊能力介绍 11-15
- 《尼尔:机械纪元》武器恶魔之秽牙属性及特殊能力介绍 11-15