加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MFC 解析XML

发布时间:2020-12-16 05:16:13 所属栏目:百科 来源:网络整理
导读:现在经常会对XML文件进行操作,怎么在MFC下去读和解析XML文件呢?直接上代码: 首先得等在stdafx.h中加入这句,以引入MSXML命名空间 #import msxml3.dll named_guids using namespace MSXML2; 然后再要使用的类的头文件加入: [cpp] view plain copy //XMLPa

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

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

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

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

[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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读