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:
BOOL GetPathedNodeWithText(const CString & strPath,
const CString &strName, IXMLDOMNode **node);
BOOL GetPathedNode(const CString &strPath, IXMLDOMNode **node);
IXMLDOMDocument* GetDocument() ;
BOOL TransformNode( CXMLFile &file, CString &result);
BOOL GetSingleNodeValue ( const CString& strPath, CString &value);
BOOL LoadFromString(const CString & str );
BOOL GetPathedNodeList(const CString &strPath, IXMLDOMNodeList **list);
BOOL GetPathedNodeList(IXMLDOMNode * node ,
const CString strPath, IXMLDOMNodeList **list);
BOOL GetSingleNodeValue(IXMLDOMNode * node,
const CString &strPath, CString &value);
BOOL GetNamedList (IXMLDOMNode *node,
const CString &name , IXMLDOMNodeList **result);
BOOL GetOneNamedNode(IXMLDOMNode *node,
const CString &name, IXMLDOMNode ** result);
BOOL GetParentNode( IXMLDOMNode * node,
IXMLDOMNode **result);
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);
BOOL CreateRootNode(const CString &tagName);
BOOL GetRootNode( IXMLDOMElement **ele);
void EmptyDocument();
BOOL GetXML(CString &xml);
void SaveFile(const CString &strFileName, IXMLDOMDocument *doc);
void SaveFile(const CString &strPathName);
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);
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);
BOOL LoadFile( const CString &strPathName);
BOOL LoadFile(const CString &strPathName, IXMLDOMDocument **doc);
protected:
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.
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