首页 技术 正文
技术 2022年11月23日
0 收藏 410 点赞 4,835 浏览 2495 个字

现在经常会对XML文件进行操作,怎么在MFC下去读和解析XML文件呢?直接上代码:

首先得等在stdafx.h中加入这句,以引入MSXML命名空间

#import <msxml3.dll> named_guids
using namespace MSXML2;

然后再要使用的类的头文件加入:

//XML Parser intellectural pointer (used in parsing XML file)
MSXML2::IXMLDOMDocument2Ptr   m_plDomDocument;
MSXML2::IXMLDOMElementPtr m_pDocRoot;

然后在初始化函数中加入对XML COM的初始化:

//intianlize XML Parser COM
::CoInitialize(NULL);
HRESULT hr = m_plDomDocument.CreateInstance(MSXML2::CLSID_DOMDocument);if(FAILED(hr))
{
_com_error er(hr);
AfxMessageBox(er.ErrorMessage());
EndDialog();
}

接着是具体的函数:

void CDemoDlg::OnBnClickedButtonOpenxml()
{
// at first ,we should clear m_strXML's contents to show new data
m_strXMLData = ""; CFileDialog fileDlg(TRUE);
if(IDOK == fileDlg.DoModal())
{
m_strXMLPath = fileDlg.GetPathName();
m_strXMLContents = "Empty document"; //just in case you don't get anything
UpdateData(FALSE); //Specify XML file name
CString strFileName = m_strXMLPath; //Convert XML file name string to something COM can handle
_bstr_t bstrFileName;
bstrFileName = strFileName.AllocSysString(); //Call the IXMLDOMDocumentPtr's load function to load XML file
variant_t vResult;
vResult = m_plDomDocument->load(bstrFileName); if(((bool)vResult) == TRUE ) //success
{
//Convert bstr to something you can use in VC++
_bstr_t bstrDocContents = m_plDomDocument->xml;
m_strXMLContents = (LPCTSTR) bstrDocContents; //get XML data and show on Edit Control
MSXML2::IXMLDOMNodePtr m_pXMLRoot = m_plDomDocument->documentElement; //first child is point
//m_strXMLData = _T(",") + DisplayXMLChildren(m_pXMLRoot); //loop to show get point's Lon and Lan and save in map<CString,CString>LonLanPoints
for(MSXML2::IXMLDOMNodePtr pChild = m_pXMLRoot->firstChild; pChild != NULL; pChild = pChild->nextSibling)
{
DisplayXMLChildren(pChild);
}
}
else
{
m_strXMLContents = "Document FAILED to load!";
} UpdateData(FALSE);
}
}//in case of overload data we use before, we should add one button for clear the data(Ready to transfrom)
CString CDemoDlg::DisplayXMLChildren(MSXML2::IXMLDOMNodePtr pParent)
{
//Display current node's name
//every parent's node name is POINT
CString strElement = ((LPCTSTR)pParent->nodeName); //output is point //loop to get data pChild's parent node is point,and point's parent node is road
for(MSXML2::IXMLDOMNodePtr pChild = pParent->firstChild; pChild != NULL; pChild = pChild->nextSibling)
{
//strElement += ((LPCTSTR)pChild->nodeName);
CString nodeName = ((LPCTSTR)pChild->nodeName);
CString Lon = _T("Lon");
CString Lan = _T("Lan");
CString currentLon,currentLan; if ( nodeName == Lon )
{
currentLon = ((LPCTSTR)pChild->text);
}
if ( nodeName == Lan )
{
currentLan = ((LPCTSTR)pChild->text);
} //add one point's Lon and Lan to map
LonLanPoints.insert(currentLon,currentLan);
}
return strElement;
}
XML结构为:
<road>
<point>
<id><id/>
<Lon></Lon>
<Lat></Lat>
</road>

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/141158.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:876 阅读:5,475
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:806 阅读:3,696
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:565 阅读:4,511
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:731 阅读:4,472
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:5,389
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,268
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

ceotheme@ceo.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站