万星星@豌豆荚 欢迎加入我们
一个吃软饭的男人!!!!!我只想写程序####
微博:http://weibo.com/wanlianwen
posts - 172,  comments - 1253,  trackbacks - 0

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

去年年底写一个配置文件(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-02-10 19:38 万连文 阅读(3118) 评论(5)  编辑 收藏 引用 所属分类: 乱七八糟

FeedBack:
# re: 狗年第一篇-MSXML类封装
2006-04-28 00:05 | weed
谢谢!正烦得很呢。。。用惯了c#读写xml,这个com太不爽了。  回复  更多评论
  
# re: 狗年第一篇-MSXML类封装
2007-12-10 17:29 | dfdfds
vknbvcbmvcbvc
nvcbnbvnvc
n\cv
n  回复  更多评论
  
# re: 狗年第一篇-MSXML类封装
2008-09-10 19:17 | Hyper
你说的设置好环境,怎么设置啊.初学者请教了  回复  更多评论
  
# re: 狗年第一篇-MSXML类封装
2008-09-10 19:32 | 万星星
就是包含msxml的头文件以及lib啊,如果是vs2005似乎已经包含了,vc6默认是没有。  回复  更多评论
  
# re: 狗年第一篇-MSXML类封装
2008-09-11 15:15 | Hyper
这个lib怎么添加啊。谢谢啊,才上大一好多都不懂  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


简历下载
联系我

<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(66)

随笔分类

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜