Home > Software engineering >  About using MSXML in MFC reads the XML file. The format of the XML file and the source code is as fo
About using MSXML in MFC reads the XML file. The format of the XML file and the source code is as fo

Time:10-06

The following is the format of the XML file
 & lt; Simplify the city information & gt; 
Beijing
Shenyang & lt; No. & gt; G1 & lt;/number & gt; Tianjin & lt; No. & gt; G2 & lt;/number & gt; Shijiazhuang & lt; No. & gt; G4 & lt;/number & gt; Hohhot & lt; No. & gt; G6 & lt;/number & gt;
Shanghai
Jinan & lt; No. & gt; G2 & lt;/number & gt; Nanjing & lt; No. & gt; G42 & lt;/number & gt; Wuhan & lt; No. & gt; G50 & lt;/number & gt; Hangzhou & lt; No. & gt; The G60 will be & lt;/number & gt;

The following is my source code (this is the C * * the h defined public structures and functions)
 
Struct citymessage
{
String name;
int x;
int y;
Cstrings direct [10].
Cstrings directnum [10].
Int short [10].
} city [33];

Void ReadXml ()
{
MSXML2: : IXMLDOMDocumentPtr pDoc;
: : CoInitialize (NULL);
Retrieves the hr=pDoc. CreateInstance (__uuidof (MSXML2: : DOMDocument40));
if (! SUCCEEDED (hr))
{
MessageBox (_T (" failed to create the DOMDocument object, \ n please check the running environment "), _T (" error "), MB_ICONERROR);
return;
}

PDoc - & gt; Load (" city information (Jane). XML ");

/*
PDoc - & gt; Put_async (VARIANT_FALSE);
VARIANT_BOOL BHR=pDoc - & gt; Load ((_variant_t) strXmlPath);
If (BHR!={VARIANT_TRUE)
MessageBox (_T (" unable to correctly read XML file "), _T (" error "), MB_ICONERROR);
return;
}
*/

MSXML2: : IXMLDOMElementPtr root=pDoc - & gt; DocumentElement;
//the name of the root node
_variant_t strRootName=root - & gt; The nodeName.
_bstr_t wstrRootName (strRootName bstrVal);
MSXML2: : IXMLDOMNodeListPtr cityList=root - & gt; GetchildNodes ();//cases, that is, for each child node is 32 provincial cities

//resolving cases of child nodes, read 32 cities information
Int ilength=cityList - & gt; Getlength ();//ilength child node number, also in this program can be set to 32
For (nodeCount=0; NodeCount & lt; Ilength; NodeCount++)
{

MSXML2: : IXMLDOMNodePtr citymessage=cityList - & gt; NextNode ();
_variant_t CityName=citymessage - & gt; GetnodeName ();
_variant_t CityValue=https://bbs.csdn.net/topics/citymessage-> GetnodeValue ();
City [nodeCount]. Name=(char *) (_bstr_t) citymessage - & gt; The text;//read the first nodeCount city name
//read the longitude and latitude and high speed connected information
ReadCity (citymessage - & gt; GetchildNodes ());
}
}

Void ReadCity (MSXML2: : IXMLDOMNodeListPtr nodeList)
{
Cstrings messagex, messagey messagename;
Int messagelength=nodeList - & gt; Getlength ();
For (messageCount=0; MessageCount & lt; Messagelength; MessageCount++)
{
MSXML2: : IXMLDOMNodePtr messagenode=nodeList - & gt; NextNode ();
_variant_t messagenodeName=messagenode - & gt; GetnodeName ();
_variant_t messagenodeeValue=https://bbs.csdn.net/topics/messagenode-> Gettext ();
BSTR bStrTemp=messagenodeName. BstrVal;
Cstrings strTemp=cstrings (bStrTemp);
SysFreeString (bStrTemp);
Cstrings strNodeName=strTemp;
If (0==strNodeName.Com pareNoCase (_T (" longitude ")))
{
Messagex=(BSTR) messagenodeName. PbstrVal;
}
If (0==strNodeName.Com pareNoCase (_T (" latitude ")))
{
Messagey=(BSTR) messagenodeName. PbstrVal;
}
If (0==strNodeName.Com pareNoCase (_T (" city name ")))
{
Messagename=(BSTR) messagenodeName. PbstrVal;
ReadLoad (messagenode - & gt; GetchildNodes ());
}
City [nodeCount]. X=_ttoi (messagex);
City [nodeCount] y=_ttoi messagey ();
City [nodeCount]. Direct [messageCount - 2]=messagename;
}
}

Void ReadLoad (MSXML2: : IXMLDOMNodeListPtr nodeList)
{
Cstrings loadistance;
Int loadlength=nodeList - & gt; Getlength ();
For (int loadnum=0; Loadnum & lt; Loadlength; Loadnum++)
{
MSXML2: : IXMLDOMNodePtr loadnode=nodeList - & gt; NextNode ();
_variant_t loadnodeName=loadnode - & gt; GetnodeName ();
_variant_t loadnodeValue=https://bbs.csdn.net/topics/loadnode-> Gettext ();
BSTR bStrTemp=loadnodeName. BstrVal;
Cstrings strTemp=cstrings (bStrTemp);
SysFreeString (bStrTemp);
Cstrings strNodeName=strTemp;
If (0==strNodeName.Com pareNoCase (_T (" id ")))
{
City [nodeCount] directnum [messageCount - 2]=(BSTR) loadnodeName. PbstrVal;
}
If (0==strNodeName.Com pareNoCase (_T (" mileage ")))
{
Loadistance=(BSTR) loadnodeName. PbstrVal;
}
City [nodeCount]. Short [messageCount - 2]=_ttoi (loadistance);
}
}

CodePudding user response:

XML plus & lt; ? The XML version="1.0" encoding="GBK"?>

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related