Click here to Skip to main content
Licence CPOL
First Posted 16 Jul 2007
Views 408,120
Downloads 2,308
Bookmarked 116 times

CXml - A Wrapping Class for MSXML 3.0/4.0/5.0/6.0

By | 21 Oct 2008 | Article
This wrapping class will try to use the latest version of MSXML in the machine, and it is easy because of using auto_ptr.

Introduction

This is a wrapping class library for MSXML 3.0/4.0/5.0/6.0 consisting of easy to use classes. The classes include CXml, CXmlNode, CXmlNodes, and CXsl.

Sample Code

Please find the sample code in the demo project.

demo.jpg

Using the Code

Copy all the files in the /CXml/*.* directory and add them into your project.

#include "Xml.h"
using namespace JWXml;

Here, I have added a namespace for the classes, you can change it as you like.

MSXML Version

The class will try to choose the version of MSXML in the following order:

hr = (hr == S_OK) ? hr : m_pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument60) );
hr = (hr == S_OK) ? hr : m_pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument30) );
hr = (hr == S_OK) ? hr : m_pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument50) );
hr = (hr == S_OK) ? hr : m_pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument40) );
hr = (hr == S_OK) ? hr : m_pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument26) );
hr = (hr == S_OK) ? hr : m_pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument) );

Classes Overview

CXml

class CXml
{
	friend class CXsl;

public:
	CXml(void);
	~CXml(void);

protected:
	MSXML2::IXMLDOMDocument2Ptr m_pDoc;
	CString m_strFilePath;
	MSXML_VERSION m_emVersion;
	std::map< CString, CString> m_mpNamespace;

	BOOL CreateInstance(void);

public:
	// Open XML file
	BOOL Open(LPCTSTR lpszXmlFilePath);

	// Create a new XML file
	BOOL Create( LPCTSTR lpszRootName = _T("xmlRoot")
		, LPCTSTR lpszPrefix = _T("")
		, LPCTSTR lpszNamespaceURI = _T("")
		);

	// Load XML string
	BOOL LoadXml(LPCTSTR lpszXmlContent);

	// save XML file
	BOOL Save(LPCTSTR lpszFilePath);

	// save XML file with formatted output
	BOOL SaveWithFormatted(LPCTSTR lpszFilePath = NULL, 
			LPCTSTR lpszEncoding = _T("UTF-8"));

	// close XML file
	void Close(void);

	CString GetXmlFile(void) const;

	// Encode the binary data into string
	CString Base64Encode( LPBYTE pBuf, ULONG ulSize);

	// Decode the string into binary data
	BOOL Base64Decode( CString strIn, LPBYTE pBuf, LONG & lSize);

	// namespace
	void AddSelectionNamespace( LPCTSTR lpszPrefix, LPCTSTR lpszURI);

	// get the root element of
	CXmlNodePtr GetRoot(void);

	// get single node by XPath
	CXmlNodePtr SelectSingleNode(LPCTSTR lpszPath);

	// get nodes by XPath
	CXmlNodesPtr SelectNodes(LPCTSTR lpszPath);

	// create node
	CXmlNodePtr CreateNode(LPCTSTR lpszName
		, LPCTSTR lpszPrefix = _T("")
		, LPCTSTR lpszNamespaceURI = _T("")
		);

	// get the current version of MSXML
	MSXML_VERSION GetVersion(void) const;
};

CXmlNode

class CXmlNode
{
	friend class CXml;
	friend class CXmlNode;
	friend class CXmlNodes;

protected:
	MSXML2::IXMLDOMNodePtr m_pNode;

	CXmlNode( MSXML2::IXMLDOMNodePtr pNode);

	BOOL _GetValue(CString & strValue) const;
	BOOL _SetValue(CString & strValue) const;

	BOOL _GetAttribute( CString & strName, CString & strValue) const;
	BOOL _SetAttribute( CString & strName IN
					  , CString & strValue IN
					  , CString & strPrefix IN
					  , CString & strNamespaceURI IN
					  ) const;

public:

	CXmlNode(void);
	CXmlNode(const CXmlNode & refNode IN);
	CXmlNode(const CXmlNodePtr pNode IN);
	~CXmlNode(void);

	CXmlNodePtr operator = (CXmlNodePtr pNode);
	CXmlNode & operator = (const CXmlNode & refNode);

	BOOL IsNull(void) const; 	// Whether the current element exist
	CString GetName(void) const;// Get the name of the current node
	CXmlNode & Detach(void);	// Detach the current node
	void Release(void);	// Release this node

	CXmlNodePtr GetChild( CString strName, BOOL bBuildIfNotExist = TRUE);
	CXmlNodePtr NewChild( CString strName );
	CXmlNodePtr GetParent(void);
	CXmlNodesPtr GetChildren();
	void AttachChild( CXmlNodePtr & pChildNode);
	void AttachChild( CXmlNode & refChildNode);
	BOOL HasChildren(void);
	BOOL RemoveChildren(void);

	CString	GetAttribute( CString strName, LPCTSTR lpszDefault = NULL) const;
	bool	GetAttribute( CString strName, bool bDefault) const;
	int		GetAttribute( CString strName, int nDefault) const;
	long	GetAttribute( CString strName, long lDefault) const;
	__int64	GetAttribute( CString strName, __int64 llDefault) const;
	float	GetAttribute( CString strName, float fDefault) const;
	double	GetAttribute( CString strName, double dDefault) const;
	DWORD	GetAttribute( CString strName, DWORD dwDefault) const;

	BOOL	SetAttribute( CString strName, LPCTSTR lpszValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, bool bValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, int nValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, long lValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, __int64 llValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, float fValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, double dValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));
	BOOL	SetAttribute( CString strName, DWORD dwValue ,
		    CString strPrefix = _T(""), CString strNamespaceURI = _T(""));

	BOOL RemoveAttribute( CString strName );

	CString	GetValue( LPCTSTR lpszDefault = NULL ) const;
	bool	GetValue( bool bDefault ) const;
	int		GetValue( int nDefault) const;
	long	GetValue( long lDefault) const;
	__int64	GetValue( __int64 llDefault) const;
	float	GetValue( float fDefault) const;
	double	GetValue( double dDefault) const;
	DWORD	GetValue( DWORD dwDefault) const;

	BOOL	SetValue( LPCTSTR lpszValue );
	BOOL	SetValue( bool bValue );
	BOOL	SetValue( int nValue );
	BOOL	SetValue( long lValue );
	BOOL	SetValue( __int64 llValue );
	BOOL	SetValue( float fValue );
	BOOL	SetValue( double dValue );
	BOOL	SetValue( DWORD dwValue );

	CXmlNodePtr SelectSingleNode(LPCTSTR lpszPath);
	CXmlNodesPtr SelectNodes(LPCTSTR lpszPath);

	CString GetOuterXml(void) const;
	CString GetInnerXml(void) const;
};

CXmlNodes

class CXmlNodes
{
	friend class CXml;
	friend class CXmlNode;
	friend class CXmlNodes;

public:
	~CXmlNodes(void);
	CXmlNodes(void);
	CXmlNodes( const CXmlNodes & refNodes );
	CXmlNodes( CXmlNodesPtr pNodes );

	CXmlNodesPtr operator = (CXmlNodesPtr pNodes);
	CXmlNodes & operator = (const CXmlNodes & refNodes);
	CXmlNodePtr operator[] ( LONG lIndex );
	CXmlNodePtr operator[] ( LPCTSTR lpszName );

	LONG GetCount(void);
	void Release(void);

	CXmlNodePtr GetItem( LONG nIndex );
	CXmlNodePtr GetItem( LPCTSTR lpszName );

protected:
	CXmlNodes(MSXML2::IXMLDOMNodeListPtr pNodeList);
	MSXML2::IXMLDOMNodeListPtr m_pNodeList;

};

CXsl

class CXsl
{
public:
	CXsl(void);
	~CXsl(void);

	// Open XSL file
	BOOL Open(LPCTSTR lpszXslFilePath);

	// close XSL file
	void Close(void);

	// transform to file
	BOOL TransformToFile( CXml & objXml, LPCTSTR lpszFilePath);

	// add a parameter
	BOOL AddParameter( LPCTSTR lpszParamName, LPCTSTR lpszParamValue, 
			LPCTSTR lpszNamespaceURI = NULL);

protected:
	MSXML2::IXSLTemplatePtr m_pIXSLTemplate;
	MSXML2::IXMLDOMDocument2Ptr m_pStyleSheet;
	MSXML2::IXSLProcessorPtr m_pIXSLProcessor;
};

History

  • v2.0
    • Created: 2007-07-16
  • v2.1
    • Added LoadXml method
    • Added GetVersion method
    • Added const for GetXXX methods
    • Defined ASSERT as ATLASSERT for ATL
    • Defined TRACE as ATLTRACE for ATL
  • V2.2
    • Added the parameter lpszRootName for CXml::Open
    • Removed CXml::GetLastError
    • Added CXml::AddNamespace
    • Added two new overrides for CXml::CreateNode with namespace support
  • V3.0
    • Added another copy constructor for CXmlNode and CXmlNodes
    • Added const modifier for some variables
    • Added CXmlNode::GetInnerXml
    • Added CXmlNode::GetOuterXml
    • Added CXml::Create
    • Changed the MSXML version for Create to 6.0 -> 3.0 -> 5.0 -> 4.0
    • Added namespace support for attributes
    • Added a new class named CXsl
  • V3.1
    • Add method CXml::SaveWithFormatted (Thanks to roel_)
    • Reuse CXml::SaveStreamToFile in CXsl::TransformToFile
    • Add CXsl::AddParameter to allow passing parameters to XSLT
    • Use std::tr1::shared_ptr if exists instead of std::auto_ptr
    • Change namespace from Generic to JWXml

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jerry.Wang

Architect
Best Brain
China China

Member

Jerry is working for the Best Brain ChangSha Office, China. He has been being interested in computer programing from his childhood. Skilled in Windows/Linux C++, Mac OS Objective-C, .Net & C#, ASP.Net, AS3/JS/PHP etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Question这个项目有人在版本低于6.0的MSXML环境下用过吗? Pinmemberdothan0091:37 25 Feb '12  
Question没有一个比较简便的demo么? Pinmemberyanzhiwei14720:47 5 Feb '12  
GeneralMy vote of 5 Pinmemberjerry_wangjh20:39 11 Nov '11  
GeneralMy vote of 5 Pinmembertankfun23:49 12 Sep '11  
Generalnew function - GetXML() PinmemberMaster^Tristar19:28 30 Mar '11  
GeneralRe: new function - GetXML() PinmemberJerry.Wang18:45 12 Apr '11  
GeneralRe: new function - GetXML() Pinmemberhazywxin25:06 26 May '11  
GeneralMy vote of 5 PinmemberJhomes11:21 14 Feb '11  
AnswerAdded two new functions: GetAttributeCount PinmemberLars [Large] Werner9:49 19 Dec '10  
Generaland GetAttributeByIndex PinmemberLars [Large] Werner9:51 19 Dec '10  
GeneralRe: and GetAttributeByIndex PinmemberJerry.Wang18:48 12 Apr '11  
QuestionIf I call _tsplitpath first then call CXmlNode::SetAttribute(strName, lpszValue). It's default parameter will have some value. why? Pinmembersilvonli21:56 15 Nov '10  
GeneralGood job, if you provide more sample is better Pinmembermaplewang0:59 26 Oct '10  
Question不支持命名空间? Pinmemberhellohuhu23:26 6 Apr '10  
AnswerRe: 不支持命名空间? PinmemberJerry.Wang16:34 7 Jul '10  
Question如何解决缩进的问题?? Pinmemberzhuliyan21:39 23 Feb '10  
AnswerRe: 如何解决缩进的问题?? PinmemberJerry.Wang16:35 8 Mar '10  
Question如何解决缩进的问题?? Pinmemberzhuliyan21:37 23 Feb '10  
GeneralHeap corruption detected PinmemberSnakefoot11:10 8 Dec '09  
QuestionSaveWithFormatted 保存为UTF-8编码文件的时候如何带签名? Pinmembertopexcel4:09 18 Nov '09  
AnswerRe: SaveWithFormatted 保存为UTF-8编码文件的时候如何带签名? PinmemberJerry.Wang15:33 18 Nov '09  
GeneralRe: SaveWithFormatted 保存为UTF-8编码文件的时候如何带签名? Pinmembertopexcel16:16 20 Dec '09  
GeneralUsage under Windows CE PinmemberYoisel5:16 14 Nov '09  
GeneralRe: Usage under Windows CE PinmemberJerry.Wang22:00 14 Nov '09  
GeneralRe: Usage under Windows CE PinmemberYoisel4:47 17 Nov '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 21 Oct 2008
Article Copyright 2007 by Jerry.Wang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid