<?xml version="1.0" encoding="gb2312"?>
<root>
<device id="10041" name="设备1">
<type>13 </type>
<typename>保护 </typename>
</device>
</root>
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);
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);
}