MSXML类封装

来源:互联网 发布:印刷自动报价软件 编辑:程序博客网 时间:2024/05/16 10:23
 MSXML类封装

目的:为了更方便的读写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文档

};


类以及示例下载