// 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><User><UserName>Test1</UserName><PassWord>MyPassWord</PassWord></User></Users>";
VARIANT_BOOL b=pDoc->loadXML((_bstr_t)strXml.c_str());
//根节点
MSXML2::IXMLDOMElementPtr spElement;
pDoc->get_documentElement(&spElement);
CComBSTR varNodeText;
CComBSTR varNodeTypeName;
string temp2;
MSXML2::IXMLDOMNodeListPtr spUserNodeList;
//获取第一层节点值。Users标签下值
spElement->get_childNodes(&spUserNodeList);
spElement->get_nodeName(&varNodeText);
CW2A str(varNodeText);
cout<<str<<endl;
//temp=varNodeText;
//cout>>temp.c_str()<<endl;
//获取节点数
long luserCount;
spUserNodeList->get_length(&luserCount);
//遍历所有节点
for (int i=0;i<luserCount;i++)
{
MSXML2::IXMLDOMNodePtr spUserChld;
MSXML2::IXMLDOMNodeListPtr spUserChldList;
//获取子节点
spUserNodeList->get_item(i,&spUserChld);
spUserChld->get_nodeName(&varNodeTypeName);
CW2A strtemp(varNodeTypeName);
cout<<strtemp<<endl;
//temp=varNodeTypeName;
//cout<<temp<<endl;
//根据子节点再获取子节点信息
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;
}
//
#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><User><UserName>Test1</UserName><PassWord>MyPassWord</PassWord></User></Users>";
VARIANT_BOOL b=pDoc->loadXML((_bstr_t)strXml.c_str());
//根节点
MSXML2::IXMLDOMElementPtr spElement;
pDoc->get_documentElement(&spElement);
CComBSTR varNodeText;
CComBSTR varNodeTypeName;
string temp2;
MSXML2::IXMLDOMNodeListPtr spUserNodeList;
//获取第一层节点值。Users标签下值
spElement->get_childNodes(&spUserNodeList);
spElement->get_nodeName(&varNodeText);
CW2A str(varNodeText);
cout<<str<<endl;
//temp=varNodeText;
//cout>>temp.c_str()<<endl;
//获取节点数
long luserCount;
spUserNodeList->get_length(&luserCount);
//遍历所有节点
for (int i=0;i<luserCount;i++)
{
MSXML2::IXMLDOMNodePtr spUserChld;
MSXML2::IXMLDOMNodeListPtr spUserChldList;
//获取子节点
spUserNodeList->get_item(i,&spUserChld);
spUserChld->get_nodeName(&varNodeTypeName);
CW2A strtemp(varNodeTypeName);
cout<<strtemp<<endl;
//temp=varNodeTypeName;
//cout<<temp<<endl;
//根据子节点再获取子节点信息
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;
}
程序输出图