MFC 解析XML

前端之家收集整理的这篇文章主要介绍了MFC 解析XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

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

  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. }

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

猜你在找的XML相关文章