Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

An MFC Wrapper for MSXML


February 2001/An MFC Wrapper for MSXML/Listing 1

Listing 1: xmlif.h — Interface to CXmlIF

/***
*  MODULE:  XmlIF.h
*  PURPOSE: Interface...XML Interface Class
*  AUTHOR:  Ben Campione
***/
#ifndef _CXMLIF_
#define _CXMLIF_

// #define _MS_XML2_           // Define import spec for MS XML DLL

#ifdef _MS_XML2_            // Import One Of The MS XML DLLs
#import "msxml3.dll" named_guids raw_interfaces_only
#define MS_XML_NS     MSXML2   // Define Name Space For MSXML2
#define XML_LOAD      load
#define XML_CRP       createProcessingInstruction
#define XML_APPCHILD  appendChild
#define XML_SELNODES  selectNodes
#define XML_CRTNODE   createNode
#define XML_CRTATTR   createAttribute
#define XML_GETNI     getNamedItem
#define XML_SETNI     setNamedItem
#else
#import "msxml.dll" named_guids raw_interfaces_only
#define MS_XML_NS     MSXML    // Define Name Space For MSXML
#define XML_LOAD      raw_load
#define XML_CRP       raw_createProcessingInstruction
#define XML_APPCHILD  raw_appendChild
#define XML_SELNODES  raw_selectNodes
#define XML_CRTNODE   raw_createNode
#define XML_CRTATTR   raw_createAttribute
#define XML_GETNI     raw_getNamedItem
#define XML_SETNI     raw_setNamedItem
#endif

const int XIF_MAX_STRING = 255;

class CXmlIF    // CXmlIF Class Interface
{
private:
  bool      m_bInitialized;
  bool      m_bLoaded;
  char      m_szURL[_MAX_PATH+1];
  char      m_szErrReason[XIF_MAX_STRING+1];
  long      m_lErrLine;
  long      m_lErrPos;

  HRESULT                       m_hr;
  MS_XML_NS::IXMLDOMDocument*   m_pDoc;
  MS_XML_NS::IXMLDOMNode*       m_pDocNode;

public:
  CXmlIF()  { Initialize();   }
  ~CXmlIF() { UnInitialize(); }

private:
  HRESULT CheckLoad(MS_XML_NS::IXMLDOMDocument* pDoc);
  void    GetParseError(MS_XML_NS::IXMLDOMParseError *pXMLError);
  void    ClearParseError(void);
  void    AppendAttribute(MS_XML_NS::IXMLDOMNode* pNode, 
                          MS_XML_NS::IXMLDOMAttribute* pAttr);

public:
  bool  Initialize(void);
  void  UnInitialize(void);
  bool  LoadDocument(const char* szURL);
  bool  SaveDocument(const char* szURL);

  bool  SetDocumentNode(void);
  bool  InsertProcessingInst(void);

  BSTR  AsciiToBSTR(const char* pszString);
  char* BSTRToAscii(const BSTR pwszString);

  bool  GetName(MS_XML_NS::IXMLDOMNode* pNode, BSTR pBName, UINT uSize);
  bool  SetText(MS_XML_NS::IXMLDOMNode* pNode, BSTR pBText);
  bool  GetText(MS_XML_NS::IXMLDOMNode* pNode, BSTR pBText, UINT uSize);
  bool  SetValue(MS_XML_NS::IXMLDOMNode* pNode, BSTR pBValue);
  bool  GetValue(MS_XML_NS::IXMLDOMNode* pNode, BSTR pBValue, UINT uSize);

  MS_XML_NS::IXMLDOMNodeList* GetChildren(MS_XML_NS::IXMLDOMNode* pNode, 
                                          const BSTR pBName = NULL, 
                                          long* pCnt = NULL);
  MS_XML_NS::IXMLDOMNode* GetChild(MS_XML_NS::IXMLDOMNode* pNode, 
                                   const BSTR pBNodeName, 
                                   UINT uOccurance);
  MS_XML_NS::IXMLDOMNode* GetChild(MS_XML_NS::IXMLDOMNodeList* pList,
                                   const long lItem);
  MS_XML_NS::IXMLDOMNode* CreateChild(MS_XML_NS::IXMLDOMNode* pNode, 
                                      const BSTR pBName,
                                      int nType = MS_XML_NS::NODE_ELEMENT);

  MS_XML_NS::IXMLDOMNamedNodeMap* CXmlIF::GetAttributeList(
                                      MS_XML_NS::IXMLDOMNode* pNode, 
                                      long* pCnt);
  MS_XML_NS::IXMLDOMNode* GetAttribute(MS_XML_NS::IXMLDOMNode* pNode, 
                                       const BSTR pBName);
  MS_XML_NS::IXMLDOMNode* GetAttribute(MS_XML_NS::IXMLDOMNamedNodeMap* pAttrs,
                                       long lIndex);
  MS_XML_NS::IXMLDOMAttribute* CreateAttribute(MS_XML_NS::IXMLDOMNode* pNode, 
                                               const BSTR pBName,
                                               const BSTR pBValue);
  bool  SetAttributeValue(MS_XML_NS::IXMLDOMNode* pNode, 
                          const BSTR pBName, BSTR pBValue);
  bool  GetAttributeValue(MS_XML_NS::IXMLDOMNode* pNode, 
                          const BSTR pBName, BSTR pBValue, UINT uSize);

  inline MS_XML_NS::IXMLDOMDocument* GetDocument(void) { return m_pDoc; }
  inline MS_XML_NS::IXMLDOMNode* GetDocumentNode(void) { return m_pDocNode; }

  inline bool IsInitialized(void) { return m_bInitialized; }
  inline bool IsLoaded(void) { return m_bLoaded; }
  
  inline const char*  GetURL(void) { return m_szURL; }
  inline const char*  GetErrReason(void) { return m_szErrReason; }
  inline long         GetErrLine(void) { return m_lErrLine; }
  inline long         GetErrPos(void) { return m_lErrPos; }
};

#endif

//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.