VC Read XML Simple Example
XML has recently been blown marvellous, a lot of data exchange, including a lot of middleware systems, WebService, software configuration files, are in the XML technology, is really a good thing, I preface, wrote a read the XML snippet, with some of MSXML's interface.
I read the configuration file for the BT software, and the XML content is as follows:
<?xml version= "1.0"?>
<bitcomet author= "Rnysmile" version= "0.1" >
<Settings>
<lastsavelocation>e:/ebook </LastSaveLocation>
<ListenPort>8497</ListenPort>
<GlobalMaxUploadRate>3072</GlobalMaxUploadRate>
<LastAdCacheClearDate>13104</LastAdCacheClearDate>
<LastTrackerKey>16547</LastTrackerKey>
<TotalUpload>303008</TotalUpload>
<TotalDownload>1180107</TotalDownload>
<LastExitTime>1132203124</LastExitTime>
</Settings>
</BitComet>
The code is as follows:
#include "stdafx.h"
#include <iostream>
using namespace Std;
#include <msxml.h>
#include <atlstr.h>
#import "Msxml.dll" raw_interfaces_only
int _tmain (int argc, _tchar* argv[])
{
:: CoInitialize (NULL);
Ccomptr<msxml::ixmldomdocument> Spdoc;
HRESULT hr = spdoc.cocreateinstance (__uuidof (MSXML::D omdocument)); Create a Document Object
Variant_bool Bflag;
hr = Spdoc->load (CComVariant (L "Bitcomet.xml"), &bflag); Load XML file
Ccomptr<msxml::ixmldomelement> spelement;
hr = Spdoc->get_documentelement (&spelement); Get root node
CComBSTR Strtagname;
hr = Spelement->get_tagname (&strtagname);
cout << "------TagName------" << Endl;
cout << CString (strtagname) << Endl;
Ccomptr<msxml::ixmldomnodelist> spnodelist;
hr = Spelement->get_childnodes (&spnodelist); Get a list of child nodes
Long Lcount;
hr = Spnodelist->get_length (&lcount);
for (long i=0; i<lcount; ++i)
{
CComVariant Varnodevalue;
Ccomptr<msxml::ixmldomnode> Spnode;
MSXML::D omnodetype NodeType;
Ccomptr<msxml::ixmldomnodelist> spchildnodelist;
hr = Spnodelist->get_item (i, &spnode); Get the node.
hr = Spnode->get_nodetype (&nodetype); Gets the type of node information
if (node_element = = NodeType)
{
hr = Spnode->get_childnodes (&spchildnodelist);
Long Childlen;
hr = Spchildnodelist->get_length (&childlen);
cout << "------nodelist------" << Endl;
for (int j=0; j<childlen; ++j)
{
Ccomptr<msxml::ixmldomnode> Spchildnode;
CComBSTR value;
hr = Spchildnodelist->get_item (j, &spchildnode);
hr = Spchildnode->get_nodename (&value); Get the name of the knot.
cout << CString (value) << Endl;
hr = Spchildnode->get_text (&value); Get the value of a node