本文主要是介绍VC++中使用XML实列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/****************************************
//必要的头文件
#import <msxml.dll> //xml
using namespace MSXML;
//****************************************
class CXMLControl
{
//公有方法
public:
_bstr_t GetNodeText(const char *URL = NULL);
_bstr_t GetNodeAttribute(const char *URL, const char *attrib);
_bstr_t GetNodeAttribute(const char *attrib);
void LoadFile(const char *file);
CXMLControl();
CXMLControl(const char *file);
bool InsertNode2(const char *parent, const char *node, const char *value, int attribCount, ...);
IXMLDOMDocumentPtr GetDocument();
bool DeleteNode(const char *URL);
bool InsertNode(const char *parent, const char *node, const char *value);
void SaveXML(const char *file);
virtual ~CXMLControl();
//公有成员
public:
bool m_isLoadFile;
_bstr_t m_CurrentNodeURL;
//私有方法
private:
void InitClass(); //初始化类
//私有成员
protected:
MSXML::IXMLDOMDocumentPtr m_pXMLDocument;
_bstr_t m_fileName;
};
class CXMLException// : CException
{
public:
_bstr_t Message;
public:
CXMLException(_bstr_t msg)
{
this->Message = msg;
}
};
#endif // !defined(AFX_XMLCONTROL_H__4CB4499C_D589_4386_8279_9932F20E8209__INCLUDED_)
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//实现文件
// XMLControl.cpp: implementation of the CXMLControl class.
//
//
#include "stdafx.h"
#include "XMLControl.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
CXMLControl::CXMLControl()
{
//初始化类
this->InitClass();
}
CXMLControl::CXMLControl(const char *file)
{
this->InitClass();
this->LoadFile(file);
}
//加载文件
void CXMLControl::LoadFile(const char *file)
{
this->m_fileName = (_bstr_t)file;
try
{
//加载文件
this->m_pXMLDocument->load(_variant_t(file));
}
catch(...)
{
_bstr_t msg = (_bstr_t)"加载文件/"" + (_bstr_t)file + (_bstr_t)"/"失败!";
throw CXMLException(msg);
}
this->m_isLoadFile = true;
}
CXMLControl::~CXMLControl()
{
this->m_pXMLDocument->Release();
}
//初始化类
void CXMLControl::InitClass()
{
//初始化COM接口
HRESULT hr_ConInitialize;
hr_ConInitialize = CoInitialize(NULL);
if(FAILED(hr_ConInitialize))
{
throw CXMLException("初始化COM接口失败!"); //初始化失败
}
//初始化Document
HRESULT hr_Document;
hr_Document = this->m_pXMLDocument.CreateInstance(__uuidof(DOMDocument));
if(FAILED(hr_Document))
{
throw CXMLException("初始化XMLDocument失败!"); //初始化失败
}
this->m_pXMLDocument->async = VARIANT_FALSE;
//初始化其它成员
this->m_isLoadFile = false;
}
_bstr_t CXMLControl::GetNodeText(const char *URL)
{
_bstr_t restr;
MSXML::IXMLDOMNodePtr node;
try
{
//获取当前节点
if(URL != NULL)
this->m_CurrentNodeURL = (_bstr_t)URL;
node = this->m_pXMLDocument->selectSingleNode(this->m_CurrentNodeURL);
restr = node->text;
}
catch(...)
{
_bstr_t msg = (_bstr_t)"找不到节点/"" + (_bstr_t)URL + (_bstr_t)"/"!";
throw CXMLException(msg);
}
return restr;
}
//获取属性
_bstr_t CXMLControl::GetNodeAttribute(const char *attrib)
{
return GetNodeAttribute(this->m_CurrentNodeURL, attrib);
}
_bstr_t CXMLControl::GetNodeAttribute(const char *URL, const char *attrib)
{
_bstr_t restr;
MSXML::IXMLDOMNodePtr node;
try
{
//获取当前节点
node = this->m_pXMLDocument->selectSingleNode((_bstr_t)URL);
}
catch(...)
{
_bstr_t msg = (_bstr_t)"找不到节点/"" + (_bstr_t)URL + (_bstr_t)"/"!";
throw CXMLException(msg);
}
try
{
//获取属性
restr = (_bstr_t)node->attributes->getNamedItem(attrib)->text;
}
catch(...)
{
_bstr_t msg = (_bstr_t)"获取属性/"" + (_bstr_t)attrib + (_bstr_t)"/"失败!";
throw CXMLException(msg);
}
return restr;
}
void CXMLControl::SaveXML(const char *file)
{
try
{
//保存XML文档
this->m_pXMLDocument->save(file);
}
catch(...)
{
_bstr_t msg = (_bstr_t)"保存到文件/"" + (_bstr_t)this->m_fileName + (_bstr_t)"/"失败";
throw CXMLException(msg);
}
}
bool CXMLControl::InsertNode(const char *parent, const char *node, const char *value)
{
MSXML::IXMLDOMNodePtr pNode;
try
{
//获取当前节点
pNode = this->m_pXMLDocument->selectSingleNode((_bstr_t)parent);
}
catch(...)
{
_bstr_t msg = (_bstr_t)"父节点/"" + (_bstr_t)parent + (_bstr_t)"/"无效!";
throw CXMLException(msg);
}
//添加
try
{
IXMLDOMElementPtr pXMLChildNode;
this->m_pXMLDocument->raw_createElement((_bstr_t)node, &pXMLChildNode);
pXMLChildNode->Puttext((_bstr_t)value);
pNode->appendChild(pXMLChildNode);
this->m_pXMLDocument->save(this->m_fileName);
return true;
}
catch(...)
{
_bstr_t msg = (_bstr_t)"添加节点/"" + (_bstr_t)node + (_bstr_t)"/"失败!";
throw CXMLException(msg);
}
return false;
}
bool CXMLControl::DeleteNode(const char *URL)
{
MSXML::IXMLDOMNodePtr pNode;
try
{
//获取当前节点
pNode = this->m_pXMLDocument->selectSingleNode((_bstr_t)URL);
}
catch(...)
{
_bstr_t msg = (_bstr_t)"节点/"" + (_bstr_t)URL + (_bstr_t)"/"无效!";
throw CXMLException(msg);
}
//添加
try
{
pNode->parentNode->removeChild(pNode);
this->m_pXMLDocument->save(this->m_fileName);
return true;
}
catch(...)
{
_bstr_t msg = (_bstr_t)"删除节点/"" + (_bstr_t)URL + (_bstr_t)"/"失败!";
throw CXMLException(msg);
}
return false;
}
//得到DocumentPtr
IXMLDOMDocumentPtr CXMLControl::GetDocument()
{
return this->m_pXMLDocument;
}
//通过可变参数加属性 可变参数的形式是 "name=value" 其中attribCount是可变参数的个数
bool CXMLControl::InsertNode2(const char *parent, const char *node, const char *value, int attribCount, ...)
{
MSXML::IXMLDOMNodePtr pNode;
try
{
//获取当前节点
pNode = this->m_pXMLDocument->selectSingleNode((_bstr_t)parent);
}
catch(...)
{
_bstr_t msg = (_bstr_t)"父节点/"" + (_bstr_t)parent + (_bstr_t)"/"无效!";
throw CXMLException(msg);
}
//添加
try
{
IXMLDOMElementPtr pXMLChildNode;
this->m_pXMLDocument->raw_createElement((_bstr_t)node, &pXMLChildNode);
pXMLChildNode->Puttext((_bstr_t)value);
//加属性
va_list ap; //参数列表
va_start(ap, attribCount); //开始参数
for(int i = 0; i < attribCount; i++)
{
char *str = va_arg(ap, char *); //得到可变参数
char *attrib_value = _tcschr(str, '='); //得到'='第一次出现的位置
char attrib_name[50];// = str;
long length = attrib_value - str;
memset(attrib_name, 0, 50);
_tcscpy(attrib_name, str);
attrib_name[length] = '/0';
attrib_value++;
//加属性
pXMLChildNode->setAttribute(attrib_name, attrib_value);
}
va_end(ap); //结束参数
pNode->appendChild(pXMLChildNode);
this->m_pXMLDocument->save(this->m_fileName);
return true;
}
catch(...)
{
_bstr_t msg = (_bstr_t)"添加节点/"" + (_bstr_t)node + (_bstr_t)"/"失败!";
throw CXMLException(msg);
}
return false;
}
这篇关于VC++中使用XML实列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!