Wrapper class for DOM interface of Windows SDK






4.44/5 (12 votes)
Oct 25, 2002
2 min read

89125

788
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.