MFC reads XML files and parses

Source: Internet
Author: User
Tags xml parser

Now often the XML file to operate, how to read and parse XML files in MFC. Directly on the code:

First, wait for the stdafx.h to add this sentence to introduce the MSXML namespace

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

Then the header file of the class to use is added:

   XML Parser intellectural pointer (used in parsing XML file)
   Msxml2::ixmldomdocument2ptr   m_pldomdocument;
   Msxml2::ixmldomelementptr   M_pdocroot;

You then add initialization to the XML COM in the initialization function:

   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 (1);
   }


Then there are the specific functions:

void Cdemodlg::onbnclickedbuttonopenxml () {//At a, we should clear M_strxml ' s contents to show new data M_strxmlda

	Ta = "";
	CFileDialog Filedlg (TRUE);
		if (Idok = = Filedlg.domodal ()) {M_strxmlpath = Filedlg.getpathname ();

		m_strxmlcontents = "Empty document";

		Just in the 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 your can use in VC + + _bstr_t =
			m_pldomdocument->xml;

			M_strxmlcontents = (LPCTSTR) bstrdoccontents;  Get XML data and show in Edit control msxml2::ixmldomnodeptr m_pxmlroot = m_pldomdocument->documentelement; The Child are 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 PC Hild = 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 CD  Emodlg::D isplayxmlchildren (msxml2::ixmldomnodeptr pparent) {//display current node ' s name//every Parent's node name is  Point CString strelement = ((LPCTSTR) pparent->nodename); The output is point//loop to get data pchild ' parent node are Point,and point ' s parent node is road for (msxml2::ixmldom Nodeptr 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 Structure:
<road>
<point>
    <id><id/>
    <Lon></Lon>
    <lat ></Lat>
</road>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.