源代码如下:(环境vs2005)希望对不熟悉的人有所帮助
#include <windows.h>
#include <msxml.h>
#import "msxml.dll" rename_namespace("xml")
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL) ;
xml::IXMLDOMDocumentPtr pDoc ;
xml::IXMLDOMElementPtr pElem ;
xml::IXMLDOMNodePtr pNode ;
xml::IXMLDOMNodeListPtr pNodeList ;
xml::IXMLDOMNamedNodeMapPtr pNodeMap;
HRESULT hr = pDoc.CreateInstance(__uuidof(xml::DOMDocument)) ; //创建文档对象
if(!SUCCEEDED(hr))
{
cout<<"无法创建DM对象!"<<endl ;
return 0 ;
}
VARIANT_BOOL bFlag;
bFlag = pDoc->load(L"BitComet.xml") ; //load xml文件
if(bFlag == S_FALSE)
{
cout<<"打开BitComet.xml出错!"<<endl ;
return 0 ;
}
hr = pDoc->get_documentElement(&pElem) ; //获取根结点
if(pElem == NULL)
{
cout<<"获取根结点失败!"<<endl ;
return 0 ;
}
BSTR strTagName = NULL;
hr = pElem->get_tagName(&strTagName) ;
cout<<"------TagName------"<<endl ;
wcout <<strTagName<<endl ;
SysFreeString(strTagName) ;
hr = pElem->get_childNodes(&pNodeList) ; //获取子结点列表
long lCount;
hr = pNodeList->get_length(&lCount) ;
for(long i=0; i<lCount; ++i)
{
xml::IXMLDOMNodePtr pTmpNode;
pNodeList->get_item(i, &pTmpNode);
hr = pTmpNode->get_attributes(&pNodeMap);
long len;
pNodeMap->get_length(&len);
for (long i=0; i<len; i++)
{
xml::IXMLDOMNodePtr pNode3;
pNodeMap->get_item(i, &pNode3);
BSTR str;
pNode3->get_nodeName(&str);
wcout<<str<<L":";
SysFreeString(str);
VARIANT variant;
pNode3->get_nodeValue(&variant);
wcout<<variant.bstrVal<<endl;
}
}
}