MFC 解析XML

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

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

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

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

@H_301_15@ [cpp] view plain copy
  1. //XMLParserintellecturalpointer(usedinparsingXMLfile)
copy
    MSXML2::IXMLDOMDocument2Ptrm_plDomDocument;
  1. MSXML2::IXMLDOMElementPtrm_pDocRoot;

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

copy
    //intianlizeXMLParserCOM
  1. ::CoInitialize(NULL);
  2. HRESULThr=m_plDomDocument.CreateInstance(MSXML2::CLSID_DOMDocument);
  3. if(Failed(hr))
  4. {
  5. _com_errorer(hr);
  6. AfxMessageBox(er.ErrorMessage());
  7. EndDialog(1);
  8. }


接着是具体的函数

copy
    voidCDemoDlg::OnBnClickedButtonOpenxml()
  1. //atfirst,weshouldclearm_strXML'scontentstoshownewdata
  2. m_strXMLData="";
  3. CFileDialogfileDlg(TRUE);
  4. if(IDOK==fileDlg.DoModal())
  5. m_strXMLPath=fileDlg.GetPathName();
  6. m_strXMLContents="Emptydocument";
  7. //justincaseyoudon'tgetanything
  8. UpdateData(FALSE);
  9. //SpecifyXMLfilename
  10. CStringstrFileName=m_strXMLPath;
  11. //ConvertXMLfilenamestringtosomethingCOMcanhandle
  12. _bstr_tbstrFileName;
  13. bstrFileName=strFileName.AllocSysString();
  14. //CalltheIXMLDOMDocumentPtr'sloadfunctiontoloadXMLfile
  15. variant_tvResult;
  16. vResult=m_plDomDocument->load(bstrFileName);
  17. if(((bool)vResult)==TRUE)//success
  18. {
  19. //ConvertbstrtosomethingyoucanuseinVC++
  20. _bstr_tbstrDocContents=m_plDomDocument->xml;
  21. m_strXMLContents=(LPCTSTR)bstrDocContents;
  22. //getXMLdataandshowonEditControl
  23. MSXML2::IXMLDOMNodePtrm_pXMLRoot=m_plDomDocument->documentElement;//firstchildispoint
  24. //m_strXMLData=_T(",")+DisplayXMLChildren(m_pXMLRoot);
  25. //looptoshowgetpoint'sLonandLanandsaveinmap<CString,CString>LonLanPoints
  26. for(MSXML2::IXMLDOMNodePtrpChild=m_pXMLRoot->firstChild;pChild!=NULL;pChild=pChild->nextSibling)
  27. DisplayXMLChildren(pChild);
  28. }
  29. }
  30. else
  31. m_strXMLContents="DocumentFailedtoload!";
  32. //incaSEOfoverloaddataweusebefore,weshouldaddonebuttonforclearthedata(Readytotransfrom)
  33. CStringCDemoDlg::DisplayXMLChildren(MSXML2::IXMLDOMNodePtrpParent)
  34. //Displaycurrentnode'sname
  35. //everyparent'snodenameisPOINT
  36. CStringstrElement=((LPCTSTR)pParent->nodeName);//outputispoint
  37. //looptogetdatapChild'sparentnodeispoint,andpoint'sparentnodeisroad
  38. for(MSXML2::IXMLDOMNodePtrpChild=pParent->firstChild;pChild!=NULL;pChild=pChild->nextSibling)
  39. //strElement+=((LPCTSTR)pChild->nodeName);
  40. CStringnodeName=((LPCTSTR)pChild->nodeName);
  41. CStringLon=_T("Lon");
  42. CStringLan=_T("Lan");
  43. CStringcurrentLon,currentLan;
  44. if(nodeName==Lon)
  45. currentLon=((LPCTSTR)pChild->text);
  46. if(nodeName==Lan)
  47. currentLan=((//addonepoint'sLonandLantomap
  48. LonLanPoints.insert(currentLon,currentLan);
  49. returnstrElement;
  50. }

[html] copy
    XML结构为:
  1. <road>
  2. point>
  3. id>/>
  4. Lon</Lat>

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...