使用MSXML.DLL读写XML;
文件顶部加入
#import "msxml3.dll";
using namespace MSXML2;
//这两句作用是,在程序的文件夹下生成MSXML3.TLH和MSXML3.TLI两个文件;
---------------------------------------------------------
创建一个XML文件;
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr XMLDOC;
MSXML2::IXMLDOMElementPtr XMLROOT;
HRESULT HR = XMLDOC.CreateInstance(_uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(HR))
{
MessageBox("faild!!");
return;
}
XMLROOT = XMLDOC->createElement("ROOT");
XMLROOT->setAttribute("ID","12345"); //设置根标签的属性;
XMLDOC->appendChild(XMLROOT);
CString TMP;
MSXML2::IXMLDOMElementPtr XMLNODE;
for(int I=0;I<10;I++)
{
TMP.Format("%d",I);
XMLNODE = XMLDOC->createElement((_bstr_t)("NODE"+TMP));
XMLNODE->put_text((_bstr_t)"NODETEXTS");//设置标签的文本内容;
XMLNODE->setAttribute("ID",(_variant_t)TMP);//设置标签的属性及内容;
XMLNODE->setAttribute("NAME","NODENAME");
XMLROOT->appendChild(XMLNODE);
}
XMLDOC->save("XMLTEST.XML");
XMLNODE.Release();
XMLROOT.Release();
XMLDOC.Release();
::CoUninitialize();
------------------------------------------------------------------
读取一个XML;
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr XMLDOC;
MSXML2::IXMLDOMElementPtr XMLROOT;
MSXML2::IXMLDOMElementPtr XMLELEMENT;
MSXML2::IXMLDOMNodeListPtr XMLNODES; //某个节点的所以字节点
MSXML2::IXMLDOMNamedNodeMapPtr XMLNODEATTS;//某个节点的所有属性;
MSXML2::IXMLDOMNodePtr XMLNODE;
HRESULT HR = XMLDOC.CreateInstance(_uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(HR))
{
MessageBox("faild!!");
return;
}
XMLDOC->load("XMLTEST.XML");
XMLROOT = XMLDOC->GetdocumentElement();//获得根节点;
XMLROOT->get_childNodes(&XMLNODES);//获得根节点的所有子节点;
long XMLNODESNUM,ATTSNUM;
XMLNODES->get_length(&XMLNODESNUM);//获得所有子节点的个数;
CString TMP;
TMP.Format("%d",XMLNODESNUM);
MessageBox(TMP);
for(int I=0;I<XMLNODESNUM;I++)
{
XMLNODES->get_item(I,&XMLNODE);//获得某个子节点;
XMLNODE->get_attributes(&XMLNODEATTS);//获得某个节点的所有属性;
XMLNODEATTS->get_length(&ATTSNUM);//获得所有属性的个数;
for(int J=0;J<ATTSNUM;J++)
{
XMLNODEATTS->get_item(J,&XMLNODE);//获得某个属性;
CString T1 = (char*)(_bstr_t)XMLNODE->nodeName;
CString T2 = (char*)(_bstr_t)XMLNODE->text;
MessageBox(T1+" = "+T2);
}
//MessageBox((_bstr_t)XMLNODE->nodeName+": "+(_bstr_t)XMLNODE->text);
}
return;
//XMLDOC->save("XMLTEST.XML");
XMLNODES.Release();
XMLNODE.Release();
XMLROOT.Release();
XMLDOC.Release();
::CoUninitialize();
---------------------------------------------------------
总结:
XML文件档类型:MSXML2::IXMLDOMDocumentPtr;
单个元素类型:MSXML2::IXMLDOMElementPtr;
单个节点类型:MSXML2::IXMLDOMNodePtr;
所有子节点类型:MSXML2::IXMLDOMNodeListPtr;
所有属性类型:MSXML2::IXMLDOMNamedNodeMapPtr;
获得节点个数的方法:get_length();
获得所有子节点的方法:get_childNodes();
获得指定节点的方法:get_item(索引号,单个节点类型变量);
写入单个节点的文本内容方法:put_text((_bstr_t)"内容");
获得单个节点的文本内容属性:XMLNODE->text;
获得单个属性的内容属性:XMLNODEATT->text;
节点名的属性为:XMLNODE->nodeName;
---------------------------------------------------------------
XML创建文件一般过程:
::CoInitialize(NULL);
HRESULT HR = XMLDOC.CreateInstance(_uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(HR))
{
MessageBox("faild!!");
return;
}
XMLROOT = XMLDOC->createElement("ROOT");//创建文件;
操作文件.....................
XMLDOC->save("XMLTEST.XML");//保存文件;
XMLNODE.Release();
XMLROOT.Release();
XMLDOC.Release();
::CoUninitialize();
------------------------------------------------------------------------------
加载文件过程序:
::CoInitialize(NULL);
HRESULT HR = XMLDOC.CreateInstance(_uuidof(MSXML2::DOMDocument30));
if(!SUCCEEDED(HR))
{
MessageBox("faild!!");
return;
}
XMLDOC->load("XMLTEST.XML");
文件操作.........................
XMLDOC->save("XMLTEST.XML");//可以保存对文件的修改;
XMLNODES.Release();
XMLNODE.Release();
XMLROOT.Release();
XMLDOC.Release();
::CoUninitialize();
转自博客园 http://