VC Read XML Simple example

Source: Internet
Author: User

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

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.