// 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