Vc parses XML notes

Source: Internet
Author: User
// ConsoleReadXml. cpp: Defines the entry point for the console application.
//

# Include "stdafx. h"
# Import "msxml4.dll"
# Include "stdlib. h"
# Include <iostream>
# Include "string"
# Include "atlbase. h"
# Include "atlstr. h"
# Include "comutil. h"
Using namespace std;
Using namespace MSXML2;

Int _ tmain (int argc, _ TCHAR * argv [])
{
CoInitialize (NULL );
Char temp [500];
MSXML2: IXMLDOMDocumentPtr pDoc;
HRESULT hr;
Hr = pDoc. CreateInstance (_ uuidof (MSXML2: DOMDocument40 ));
If (FAILED (hr ))
{
Cout <"msxml4.dll can't find" <endl;
Return 1;
}
String strXml = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Users> <User> <UserName> Test1 </UserName> <PassWord> MyPassWord </PassWord> </User> <UserName> Test1 </UserName> <PassWord> myPassWord </PassWord> </User> </Users> ";


VARIANT_BOOL B = pDoc-> loadXML (_ bstr_t) strXml. c_str ());

// Root Node
MSXML2: IXMLDOMElementPtr spElement;

PDoc-> get_documentElement (& spElement );


CComBSTR varNodeText;
CComBSTR varNodeTypeName;
String temp2;

MSXML2: IXMLDOMNodeListPtr spUserNodeList;

// Obtain the value of the first node. Users label Value
SpElement-> get_childNodes (& spUserNodeList );
SpElement-> get_nodeName (& varNodeText );
CW2A str (varNodeText );
Cout <str <endl;
// Temp = varNodeText;
// Cout> temp. c_str () <endl;

// Obtain the number of nodes
Long luserCount;
SpUserNodeList-> get_length (& luserCount );

// Traverse all nodes
For (int I = 0; I <luserCount; I ++)
{
MSXML2: IXMLDOMNodePtr spUserChld;
MSXML2: IXMLDOMNodeListPtr spUserChldList;

// Obtain the subnode
SpUserNodeList-> get_item (I, & spUserChld );
SpUserChld-> get_nodeName (& varNodeTypeName );
CW2A strtemp (varNodeTypeName );
Cout <strtemp <endl;
// Temp = varNodeTypeName;
// Cout <temp <endl;

// Obtain the subnode information based on the subnode
SpUserChld-> get_childNodes (& spUserChldList );
Long luserChldcnt;
SpUserChldList-> get_length (& luserChldcnt );
For (int j = 0; j <luserChldcnt; j ++)
{
MSXML2: IXMLDOMNodePtr endNode;
SpUserChldList-> get_item (j, & endNode );
EndNode-> get_nodeName (& varNodeTypeName );
// Temp = varNodeTypeName;
// Cout <temp <endl;

CW2A printstr (varNodeTypeName );
Cout <printstr <"--";

EndNode-> get_text (& varNodeText );

CW2A printstr2 (varNodeText );
Cout <printstr2 <endl;
EndNode. Release ();

}
SpUserChldList. Release ();
SpUserChld. Release ();

}
SpUserNodeList. Release ();
SpElement. Release ();
PDoc. Release ();


CoUninitialize ();
Return 0;
}

Program output diagram

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.