1、头文件载入 #import <msxml3.dll>
2、可能出现的问题及解决方法:
#import <msxml3.dll> 导致 LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
3、创建XML文件
void CTestDlg1023Dlg::OnBnClickedBcreatexml()
{
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot;
//创建DOMDOCUMENT对象
HRESULT hr=pDoc.CreateInstance(_uuidof(MSXML2::DOMDocument40));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
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("WuHan");//节点值
childNode->setAttribute("population","8,000,000");//属性名,属性值
childNode->setAttribute("area","10000");
xmlRoot->appendChild(childNode);
pDoc->raw_createElement((_bstr_t)(char*)"City", &childNode);
childNode->Puttext("ShangHai");
childNode->setAttribute("population","12,000,000");
childNode->setAttribute("area","12000");
xmlRoot->appendChild(childNode);
pDoc->raw_createElement((_bstr_t)(char*)"City", &childNode);
childNode->Puttext("Hangzhou");
childNode->setAttribute("population","8,500,000");
childNode->setAttribute("area","16000");
xmlRoot->appendChild(childNode);
pDoc->save("E:\\Test.xml");
}
4、读取XML文件
void CTestDlg1023Dlg::OnBnClickedBpharsexml()
{
MSXML2::IXMLDOMDocumentPtr m_spDoc;
HRESULT hr = m_spDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
//char *pPath="E:\\Test.xml";
//CComBSTR Path = CComBSTR(pPath);
将XML文件导入内存中的文档对象
//VARIANT_BOOL success=m_spDoc->load(CComVariant(Path));
m_spDoc->load("E:\\Test.xml");
CComPtr<MSXML2::IXMLDOMNode> spNode;
CComPtr<MSXML2::IXMLDOMNodeList> spChildren;
//获取文档中的所有一级结点(结点名= RequiredNodeName)
m_spDoc->documentElement->get_childNodes(&spChildren);
long NumChildren = 0;
spChildren->get_length(&NumChildren);
CComBSTR NodeName;
int i = 0;
//spChildren->item[i]->get_nodeName(&NodeName);
//遍历结点名字
for (i = 0; i < NumChildren; i++)
{
//NodeName.Empty();
spChildren->item[i]->get_nodeName(&NodeName);
CString strNodename = (char*)(_bstr_t)NodeName;//City
VARIANT varVal;
spChildren->item[i]->get_nodeTypedValue(&varVal);
CString strCityName = (char*)(_bstr_t)varVal;//WuHan、ShangHai、Hangzhou
TRACE("【%s】=%s\n",strNodename,strCityName);//在“输出”面板输出
//获取每个城市的属性名和属性值
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
spChildren->item[i]->get_attributes(&pAttrs);
long nAttrCount ;
pAttrs->get_length(&nAttrCount);
for(int i = 0 ; i < nAttrCount ; i++)
{
pAttrs->get_item(i,&pAttrItem);
//我们可以通过函数get_nodeName,get_nodeTypedValue得到属性名和属性值,也可以直接得到
CString strAttrName = (char*)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue = (char*)(_bstr_t)pAttrItem->nodeTypedValue;
TRACE("Attr[%d]:%s=%s\t",i+1,strAttrName,strAttrValue);//在“输出”面板输出
}
TRACE("\n");//在“输出”面板输出
}
if (i < NumChildren)
{
spNode = spChildren->item[i];
}
}
demo下载地址:http://download.csdn.net/detail/brk1985/8073939
1、VC_XML http://blog.csdn.net/benny5609/article/details/2552504
2、如何用MSXML2操作XML配置文件 http://blog.csdn.net/wishfly/article/details/1892600