VC++中使用XML实列

2024-03-07 18:48
文章标签 xml c++ 使用 实列

本文主要是介绍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实列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/784474

相关文章

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添