blog

枪手亨利

博客园 首页 新随笔 联系 订阅 管理

狗年第一篇-MSXML类封装

本命年没穿红内裤,大年初一被狗咬,惨,所以大家本命年切记要穿,否则会倒霉的!!!

去年年底写一个配置文件(XML格式),第一次直接用MSXML,勉强实现,代码冗余且杂乱,刚写好,需求变了些,代码很难维护,所以自己写了一个封装类,接口层都是STL类,用起来比较方便,这样可以剩了类型转换的麻烦,适合不熟悉COM的程序员。只封装了部分功能,但是XML基本操作都可以实现。

目的:为了更方便的读写XML文档,对MSXML4.0类进行封装
            主要解决一些接口参数转换问题
            使用前确保已经安装好MSXML4.0且设置好环境
            适合VC开发语言

下面是类:

class WLWXML
{
public:
 WLWXML()
 {
  m_pIXMLDoc = NULL;
 }
 ~WLWXML()
 {
  SafeReleaseXMLDoc();
 }

 // 创建一个XML文档,成功返回true,失败返回false
 bool ConstructXMLFile();

 // 从文件加载一个XML文件,加载成功返回true,加载失败返回false
 bool LoadFromXMLFile(const std::string& fileName);

 // 保存XML文件到fileName,成功返回true,失败返回false
 bool SaveToXMLFile(const std::string& fileName);

 // 安全释放XML文档
 void SafeReleaseXMLDoc();

 // 获得XML文件内容
 void GetXML(std::string& strXML);

 // 在文档pIParentElem元素下添加nodeName节点,值为nodeValue
 bool AppendMemberNode( const std::string& nodeName,
        const std::string& nodeValue,
        IXMLDOMElement*    pIParentElem,
        IXMLDOMNode**    ppOutNewChild=NULL);
 bool AppendMemberNode( const std::string& nodeName,
        int       nodeValue,
        IXMLDOMElement*    pIParentElem,
        IXMLDOMNode**    ppOutNewChild=NULL);
 bool AppendMemberNode( const std::string& nodeName,
        long      nodeValue,
        IXMLDOMElement*    pIParentElem,
        IXMLDOMNode**    ppOutNewChild=NULL);
 bool AppendMemberNode( const std::string& nodeName,
        double      nodeValue,
        IXMLDOMElement*    pIParentElem,
        IXMLDOMNode**    ppOutNewChild=NULL);
 bool AppendMemberNode( const std::string& nodeName,
        bool      nodeValue,
        IXMLDOMElement*    pIParentElem,
        IXMLDOMNode**    ppOutNewChild=NULL);

 // 为元素pIParentElem添加属性
 bool AppendAttributeNode(const std::string& nodeName,
        const std::string& nodeValue,
        IXMLDOMElement*  pIParentElem);
 bool AppendAttributeNode(const std::string& nodeName,
        int     nodeValue,
        IXMLDOMElement*  pIParentElem);
 bool AppendAttributeNode(const std::string& nodeName,
        long    nodeValue,
        IXMLDOMElement*  pIParentElem);
 bool AppendAttributeNode(const std::string& nodeName,
        double    nodeValue,
        IXMLDOMElement*  pIParentElem);
 bool AppendAttributeNode(const std::string& nodeName,
        bool    nodeValue,
        IXMLDOMElement*  pIParentElem);

 // 获取pIParentElem元素下nodeName节点的值
 bool GetNodeValue( IXMLDOMNode*  pIParentElem,
       const std::string& nodeName,
       std::string&  nodeValue);
 bool GetNodeValue( IXMLDOMNode*  pIParentElem,
       const std::string& nodeName,
       int&    nodeValue);
 bool GetNodeValue( IXMLDOMNode*  pIParentElem,
       const std::string& nodeName,
       long&    nodeValue);
 bool GetNodeValue( IXMLDOMNode*  pIParentElem,
       const std::string& nodeName,
       double&    nodeValue);
 bool GetNodeValue( IXMLDOMNode*  pIParentElem,
       const std::string& nodeName,
       bool&    nodeValue);

 // 获得节点pIParentElem的属性
 bool GetAttributeNode(IXMLDOMNode*  pIParentElem,
        const std::string& nodeName,
        std::string&  nodeValue);
 bool GetAttributeNode(IXMLDOMNode*  pIParentElem,
        const std::string& nodeName,
        int&    nodeValue);
 bool GetAttributeNode(IXMLDOMNode*  pIParentElem,
        const std::string& nodeName,
        long&    nodeValue);
 bool GetAttributeNode(IXMLDOMNode*  pIParentElem,
        const std::string& nodeName,
        double&   nodeValue);
 bool GetAttributeNode(IXMLDOMNode*  pIParentElem,
        const std::string& nodeName,
        bool&    nodeValue);

 // 获得文档元素
 IXMLDOMElement* GetDocElem();
 
 // 获得节点的nodeName孩子
 IXMLDOMNode* GetChildNode(IXMLDOMNode*  pIParentElem,
         const std::string& nodeName,
         std::string&  nodeValue);
protected:
private:
 IXMLDOMDocument2*    m_pIXMLDoc;  // XML文档

};


类以及示例下载

posted on 2006-04-07 17:45  henry  阅读(493)  评论(0)    收藏  举报