Click here to Skip to main content
15,881,600 members
Articles / Desktop Programming / MFC
Article

Wrapper class for DOM interface of Windows SDK

Rate me:
Please Sign up or sign in to vote.
4.44/5 (13 votes)
24 Oct 20022 min read 88.1K   788   26   15
A wrapper class of IXMLDOMDocument and other interfaces of XML in Windows.

Introduction

Windows SDK provides an IXMLDocument and a series of other interfaces for XML processing. The object is very powerful and easy to use. I wrapped this interface into a class for more easy use in my own project using XML. I dealt with most of the errors in the class, not in the user’s code. I think it will be a good start point for you to make further usage of XML using Visual C++.

Road map

I wrote a program using XML to deal the business logic, using Winsocket. I found that XML is very powerful to represent data. I think it is a good idea to use XML in your program, for your data structure is variant. You need not change the serialize function. You need not waste most of your time to debug your program because you change your data structure. You need not prepare your data repeatedly. You just need one function.

In a real program, most of the data transfer from one place to another place is from one class to another class. Or even from one program to another program. To make the date transfer easily and promptly, we need a common way to exchange the data among these requirements. XML is a good method. I've been writing programs using Visual C++ for several years. I found that Windows SDK provides a series of interfaces to use with XML. I found, it is powerful. But it is so ugly that I have to write SUCCEEDED(hr) elsewhere. It makes my program look so long that it confused the business logic. I decided to wrap the class to simplify the dilemma. I did it.

The following is the class you can use elsewhere. I'll feel happy if you mentioned my name in your program’s credits.

Explanation of my wrapper class

class CXMLFile  
{
public:
    /*
        find a node with specified text in the given path.
        Input:
          const CString &strPath: the path, i.e. "//catalog/db
          const CString &strName: text the node will be contained.
        Output:
          IXMLDOMNode  **node: node found by the function according, 
          NULL for not found
        Return:
          FALSE if some error found.
*/
    BOOL GetPathedNodeWithText(const CString & strPath, 
                const CString &strName, IXMLDOMNode **node);


    /*
Get the pahted node from current document  
Input:
    const CString &strPath:   Path used to find the node
Output:
    IXMLDOMNode **node :    node found by this funciton
Return:
    FALSE for not found, TRUE for OK
*/
    BOOL GetPathedNode(const CString &strPath, IXMLDOMNode **node);

    /*
    Get the current actived document.
    Return :
        The activated document, NULL for none
    */
    IXMLDOMDocument* GetDocument() ;
    /*
        Transfrom the node of current file’s content according to XSL given.
    Input:
CXMLFile &file:  parsed XSL file 
    Output:
        CString &result:  result string if the translate occurred in the function.
    Return :
        FALSE is any error occurred. This function may be 
        throw exception according to DOM interface.
*/
    BOOL TransformNode(  CXMLFile &file, CString &result);

/*
    Get the value of a node with given path , here value means text node of XML
    Input:
        const CString &strPath:  path used to find the value of node
    Output:
        CString & value : the value found
    Return :
        TRUE for OK , false for any error occured
*/
    BOOL GetSingleNodeValue ( const CString& strPath, CString &value);
/*
  Parse the string to a document of XML
Input: 
    const CString & str :  the string will be parsed by the this function 
Return:
    FALSE will be returned if any error occurred 
    in the function, TRUE for program is OK
*/
    BOOL LoadFromString(const CString & str );

/* 
  Retrieve all nodes in the document with same path
Input :
const CString &strPath.: path refer to the nodes
Output:
IXMLDOMNodeList **list:  the list of node will be found in the current document
*/
    BOOL GetPathedNodeList(const CString &strPath, IXMLDOMNodeList **list);
/*
    Retrive all node under given node in the current document with same path
    Input:
        IXMLDOMNode * node: the given node 
       Const Cstirng & strPath : the path to retrieve
    Output:
IXMLDOMNodeList **list:  the list of node will be found in the current document
*/
    BOOL GetPathedNodeList(IXMLDOMNode * node , 
      const CString strPath, IXMLDOMNodeList **list);
/*
    Get the text node’s value of given node with same path:
    Input:
        IXMLDOMNode * node: the given node 
       Const Cstirng & strPath : the path to retrieve
    Output:
        The specified type of value.
*/
    BOOL GetSingleNodeValue(IXMLDOMNode * node, 
         const CString &strPath, CString &value);
/*
 Get the named node in a list under given node and tag name
*/
    BOOL GetNamedList (IXMLDOMNode *node, 
        const CString &name , IXMLDOMNodeList **result);
/*
 Get the first node in the document with given name under given node .
*/
    BOOL GetOneNamedNode(IXMLDOMNode *node, 
       const CString &name, IXMLDOMNode ** result);
    BOOL GetParentNode( IXMLDOMNode * node, 
       IXMLDOMNode **result);
/*
  Get the text value of given node’s child node
*/
    BOOL GetChildNodeValue(IXMLDOMElement *node, 
       const CString &nodeName,COleDateTime &date);
    BOOL GetChildNodeValue(IXMLDOMElement *node, 
       const CString &nodeName,double &d);
    BOOL GetChildNodeValue(IXMLDOMElement *node, 
       const CString &nodeName,float &f);
    BOOL GetChildNodeValue(IXMLDOMElement *node, 
       const CString &nodeName,long &l);
    BOOL GetChildNodeValue(IXMLDOMElement *node, 
       const CString &nodeName,int &i);
/*
 Create a root node in an empty doucment
*/
    BOOL CreateRootNode(const CString &tagName);
/*
    Get the root node of current document 
*/
    BOOL GetRootNode( IXMLDOMElement **ele);
/*
    Delete all contents in the document and form a empty document
*/
    void EmptyDocument();
/*
 Get the string of XML in the current document
*/
    BOOL GetXML(CString &xml);
/*
    Save the current document to specified file
*/
    void SaveFile(const CString &strFileName, IXMLDOMDocument *doc);
    void SaveFile(const CString &strPathName);
/*
    Add a new node under given node , using different data type
*/
    BOOL AddNewNode(IXMLDOMNode *node , 
         const CString & name, const CString &value);
    BOOL AddNewNode( IXMLDOMNode **result,IXMLDOMNode *node, 
         const CString &name, const COleDateTime &date);
    BOOL AddNewNode( IXMLDOMNode **result,IXMLDOMNode *node, 
         const CString &name, const double&d);
    BOOL AddNewNode( IXMLDOMNode **result,IXMLDOMNode *node, 
         const CString &name, const float &f);
    BOOL AddNewNode( IXMLDOMNode **result,IXMLDOMNode *node, 
         const CString &name,  const long &l);
    BOOL AddNewNode( IXMLDOMNode **result,IXMLDOMNode *node, 
         const CString &name, const int &i);
    BOOL AddNewNode( IXMLDOMNode *node, const CString &name, 
         const COleDateTime &date);
    BOOL AddNewNode( IXMLDOMNode *node, const CString &name, const double&d);
    BOOL AddNewNode( IXMLDOMNode *node, const CString &name, const float &f);
    BOOL AddNewNode( IXMLDOMNode *node, const CString &name,  const long &l);
    BOOL AddNewNode( IXMLDOMNode *node, const CString &name, const int &i);
/*
      Get the value of current text node
*/
    BOOL GetNodeValue(IXMLDOMElement *node, double &d);
    BOOL GetNodeValue(IXMLDOMElement *node, float &f);
    BOOL GetNodeValue(IXMLDOMElement *node,COleDateTime &date);
    BOOL GetNodeValue(IXMLDOMElement *node, long &l);
    BOOL GetNodeValue(IXMLDOMElement *node,int &i);
    BOOL GetNamedNodeList(const CString &name,IXMLDOMNodeList **result);
    BOOL AddAttribute(IXMLDOMElement *element, 
         const CString &attName,const  _variant_t &value);
    BOOL SetNodeText(IXMLDOMNode *node , const CString & text);
    BOOL AddNewNode ( IXMLDOMNode **result, 
         IXMLDOMNode * node , const CString 
    BOOL AddAttribute(IXMLDOMElement * element, 
         const CString &attName, const CString &attValue);
    BOOL GetChildNodeValue( IXMLDOMElement * node, 
         const CString & nodeName, CString & value);
    CXMLFile();
    virtual ~CXMLFile();
    BOOL GetNodeValue(IXMLDOMElement * node , CString &value );
    BOOL GetAttributeValue ( IXMLDOMElement * node, 
         const CString &attribName, CString &value);
    /*
    Load file to the current document according in specified path.
        Const CString &strPathName; the path of file
        IXMLDOMDocument **doc: current document parsed
    */
    BOOL LoadFile( const CString &strPathName);
    BOOL LoadFile(const CString &strPathName, IXMLDOMDocument **doc);
protected:
    //the current document
    IXMLDOMDocument* m_pXML;

};

Usage

Using this class is very simple. If you operate with files, declare an object of this class, and go on.

CXMLFile file;
File.LoadFile(…..);

If you want to operate with CString:

CXMLFile file;
File.LoadFromString (….);

And then , you can do other operations.

Hope this class will be useful. Thanks.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
China China
I'm write program from 1990. My research field is CAG,CAD and Image processing. I select C/C++, ASP, Java, XML as my usaully developing tools. Occasional , write code in Delphi and VB. I'm using Visual C++ from 1996. If you have anything unclear, e-mail to :zhou_cn123@sina.com Software Engineering and CAD is my mainly research program.

You also can reach me on msn: zhoujohnson@hotmail.com

Comments and Discussions

 
GeneralXml Encoding Pin
JensB18-Aug-04 1:52
JensB18-Aug-04 1:52 
GeneralHello Pin
Rassul Yunussov12-Aug-04 1:05
Rassul Yunussov12-Aug-04 1:05 
GeneralRe: Hello Pin
Johnson Zhou12-Aug-04 18:36
Johnson Zhou12-Aug-04 18:36 
General2 parameters Pin
LuisRey6-May-04 5:13
LuisRey6-May-04 5:13 
GeneralLNK2019 Pin
Grandmasta29-Mar-04 20:15
Grandmasta29-Mar-04 20:15 
GeneralMemory leak also in GetNodeValue... Pin
lassi_h9-Feb-04 22:27
lassi_h9-Feb-04 22:27 
GeneralMemory leak in GetPathedNodeList Pin
lassi_h9-Feb-04 21:37
lassi_h9-Feb-04 21:37 
Questioncan you give a demo example? Pin
liuty200616-May-03 4:28
liuty200616-May-03 4:28 
AnswerRe: can you give a demo example? Pin
Johnson Zhou16-May-03 20:23
Johnson Zhou16-May-03 20:23 
GeneralCategory switch -> XML Pin
Ravi Bhavnani25-Oct-02 4:59
professionalRavi Bhavnani25-Oct-02 4:59 
GeneralRe: Category switch -> XML Pin
Anonymous5-Nov-02 18:05
Anonymous5-Nov-02 18:05 
GeneralRe: Category switch -> XML Pin
Ravi Bhavnani6-Nov-02 3:35
professionalRavi Bhavnani6-Nov-02 3:35 
Generalsmart pointers Pin
25-Oct-02 1:48
suss25-Oct-02 1:48 
GeneralRe: smart pointers Pin
Anonymous25-Oct-02 2:22
Anonymous25-Oct-02 2:22 
Questionhi ... ??? Pin
eXplodus25-Oct-02 0:43
eXplodus25-Oct-02 0:43 
Confused | :confused: is this class useable to display a xmlfile? or only to parse it?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.