1、依赖库
在stdafx.h里面加入下面几行代码
#import "msxml3.dll"
#define XMLDocPtr MSXML2::IXMLDOMDocumentPtr
#define XMLNodePtr MSXML2::IXMLDOMNodePtr
#define XMLNodeListPtr MSXML2::IXMLDOMNodeListPtr
未安装msxml3.dll库,百度https://xiazai.zol.com.cn/detail/45/442490.shtml
2、解析
#include "stdafx.h"
bool XmlParse(CString csXml)
{
::CoInitialize(0);
XMLDocPtr doc2;
HRESULT HR = doc2.CreateInstance(_uuidof(MSXML2::DOMDocument60));
if (FAILED(HR))
{
AfxMessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
}
//xml文件导入
//CString xmlfilePath = "C:\\Users\\Administrator\\Desktop\\xml.txt";
//doc2->load((LPCTSTR)xmlfilePath);
//xml字符串导入
bool res = doc2->loadXML((_bstr_t)csXml);
if (!res)
{
//AfxMessageBox("XML数据解析失败!");
}
MSXML2::IXMLDOMNodeListPtr TablesNodelist;
XMLNodePtr NewDataSet = doc2->selectSingleNode((LPCTSTR)"//NewDataSet");//获得单个节点的信息
NewDataSet->get_childNodes(&TablesNodelist);//获取NewDataSet全部子节点
long TablesCount = 0;
TablesNodelist->get_length(&TablesCount);//所有子节点的个数
for (int i = 0; i < TablesCount; ++i)
{
MSXML2::IXMLDOMNodePtr TablesNode;
TablesNodelist->get_item(i, &TablesNode);//顺序获取节点
//BSTR str = (BSTR)alloca(64);
//node->get_text(&str); //获取Tables节点所有的值
MSXML2::IXMLDOMNodeListPtr TablesChildNodelist;
TablesNode->get_childNodes(&TablesChildNodelist);
long TablesChildLength = 0;
TablesChildNodelist->get_length(&TablesChildLength); //Tables子节点
BSTR strTablesChildValue = (BSTR)alloca(64);
BSTR strTablesChildName = (BSTR)alloca(64);
vector<CString> vecData(4,"");
for (int i = 0; i < TablesChildLength; ++i)
{
MSXML2::IXMLDOMNodePtr TablesChildNode;
TablesChildNodelist->get_item(i, &TablesChildNode);
TablesChildNode->get_nodeName(&strTableChildsName);
TablesChildNode->get_text(&strTablesChildValue);
CString csTablesChildName = _com_util::ConvertBSTRToString(strTablesChildName);
if ("MODELNAME" == csTablesChildName)
vecData[0] = _com_util::ConvertBSTRToString(strTablesChildValue);
else if ("MODELTYPE" == csTablesChildName)
vecData[1] = _com_util::ConvertBSTRToString(strTablesChildValue);
else if ("POINTDATA" == csTablesChildName)
vecData[2] = _com_util::ConvertBSTRToString(strTablesChildValue);
else if ("SCODE" == csTablesChildName)
vecData[3] = _com_util::ConvertBSTRToString(strTablesChildValue);
}
}
/*
MSXML2::IXMLDOMNamedNodeMapPtr nodemap;
TablesNode->get_attributes(&nodemap); //获取节点的属性
long length = 0;
nodemap->get_length(&length);
CString t1 = "";
CString t2 = "";
for (int n = 0; n < length; ++n) //循序输出属性
{
nodemap->get_item(n, &TablesNode);
t1 = (char*)(_bstr_t)TablesNode->nodeName;
t2 = (char*)(_bstr_t)TablesNode->text;
}
*/
return true;
}
原XML文件
<?xml version="1.0" encoding="utf-8"?>
<DataTable xmlns="http://tempuri.org/">
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Tables id="Tables1" rowOrder="0">
<MODELNAME>DFCM</MODELNAME>
<MODELTYPE>1</MODELTYPE>
<POINTDATA>-41067,-20083</POINTDATA>
<SCODE>6cb54ae56575</SCODE>
</Tables>
<Tables id="Tables2" rowOrder="1">
<MODELNAME>DFCM1</MODELNAME>
<MODELTYPE>1</MODELTYPE>
<POINTDATA>-63113,-26001</POINTDATA>
<SCODE>a833f8a534e</SCODE>
</Tables>
<Tables id="Tables3" rowOrder="2">
<MODELNAME>DFCM2</MODELNAME>
<MODELTYPE>1</MODELTYPE>
<POINTDATA>-68436,-18291</POINTDATA>
<SCODE>e03928a58</SCODE>
</Tables>
</NewDataSet>
</diffgr:diffgram>
</DataTable>