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>