CXMLTool

Manages XML documents.

[ XMLSample | Source | Keywords | Summary | Ancestors | All Members | Descendants ]

Quick Index

DESCRIPTION
USAGE
EXAMPLE
ADMINISTRATIVE
SEE ALSO

Class Summary

class CXMLTool

{

public:
static HRESULT MakeXMLBuffer(IXMLDOMDocument *pXMLDOMDocument, CString &szXMLBuffer, int nIndentStep);
static HRESULT MakeXMLBuffer(IXMLDOMNode *pParentNode, CString &szXMLBuffer, int nIndentValue, int nIndentStep);
static HRESULT CreateXMLNode(IXMLDOMDocument* pDoc, DOMNodeType type, BSTR bstrName, IXMLDOMNode **node);
static HRESULT DeleteXMLNode(IXMLDOMNode *pNode);
static HRESULT CreateXMLNodeChild(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, IXMLDOMNode **pNode);
static HRESULT CreateXMLNodeChildText(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, LPCTSTR lpszNodeText, IXMLDOMNode **pNode);
static BOOL LoadXMLDocumentFromMemory(IXMLDOMDocument *pXMLDOMDocument, BSTR pBXMLText, char *lpszErrorMessage);
static BOOL CheckTagPresence(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName);
static BOOL GetTagText(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName, CString &szTagText);
static BOOL GetXMLParseError(IXMLDOMDocument *pXMLDOMDocument, CString &szErrorReason, long &nErrorCode, long &nLineNumber);
static BOOL GetChildTagText(IXMLDOMNode *pParentNode, LPCTSTR lpszTagName, CString &szTagText);
static BOOL GetTopLevelNode(IXMLDOMDocument *pXMDOMDocument, IXMLDOMNode **ppTopLevelNode);
static BOOL CopyNode(IXMLDOMNode *pSourceNode, IXMLDOMNode *pDestinationNode, IXMLDOMDocument *pXMLDOMDocument);
static BOOL GetFirstNodeWithName(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszNodeName, IXMLDOMNode **ppNode);
static CString GetTagName(IXMLDOMNode *pNode);
static CString GetXMLPath(IXMLDOMNode *pNode);
protected:
}; // CXMLTool

Back to the top of CXMLTool

DESCRIPTION

This class provides high-level access to the Microsoft's XMLDOM Document Object Model (parser/analyser for XML documents). It wraps the complex APIs to make them easy to use.

Most functions implemented here are very simple. They are examples of using XMLDOM API in VC++, as well as integration with MFC's data type CString (if your application doesn't use MFC, you better not use CXMLTool).

Back to the top of CXMLTool

USAGE

This class has only static function members: you can call them directly.

Back to the top of CXMLTool

EXAMPLE

    // =======================================================================
    // Transform XML with XSL
    // =======================================================================
    BSTR pBXMLString = NULL;
    VARIANT vObject;
    IXMLDOMDocument *pOutputXMLDocument = NULL;
    IDispatch *pDisp = NULL;
    hResult = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pOutputXMLDocument);

hResult = pOutputXMLDocument->QueryInterface(IID_IDispatch, (void **)&pDisp); vObject.vt = VT_DISPATCH; vObject.pdispVal = pDisp;

// Use transformNodeToObject hResult = pXMLDocument->transformNodeToObject(pXMLStyle, vObject);

if (!FAILED(hResult)) { CXMLTool::MakeXMLBuffer(pOutputXMLDocument, szXMLString, nIndentStep); }

// ======================================================================= // Print the transformed string // ======================================================================= cout << (LPCTSTR)szXMLString;

Back to the top of CXMLTool

ADMINISTRATIVE

Author Emmanuel KARTMANN

Date Monday 1/24/00 20:19:55 AM

Back to the top of CXMLTool

SEE ALSO

Extensible Markup Language (XML) at Microsoft's Web Site
Using the XMLDOMDocument Object
Using the XMLDOMNode Object
Using the XMLDOMNodeList Object
Using the XMLDOMNamedNodeMap Object

Back to the top of CXMLTool

HRESULT MakeXMLBuffer(IXMLDOMDocument *pXMLDOMDocument, CString &szXMLBuffer, int nIndentStep);

Purpose: Build the XML source (flat text file) of an XMLDOM (tree data structure).

Parameters:

in pXMLDOMDocument
XML Document (the tree data structure)
out szXMLBuffer
built buffer (flat text)
in nIndentStep
indentation step for every XML node (0 for none)

Return value : HRESULT = S_OK for success, otherwise an error occured

Description : This function call recursive function MakeXMLBuffer() below.

CAUTION: this function doesn't generate standard XML. It's rather a good example on how to walk the XML tree to produce your own text file format. For example, empty tags are not terminated by / (e.g. "<EMPTY/>"). Instead, they are shown by a start and end tag (i.e. "<EMPTY></EMPTY>").

    static HRESULT MakeXMLBuffer(IXMLDOMDocument *pXMLDOMDocument, CString &szXMLBuffer, int nIndentStep);

Back to the top of CXMLTool

HRESULT MakeXMLBuffer(IXMLDOMNode *pParentNode, CString &szXMLBuffer, int nIndentValue, int nIndentStep);

Purpose: Build the XML source (flat text file) of an XMLDOM node (and its children)

Parameters:

in pParentNode
current XML node in the XML data structure (tree)
out szXMLBuffer
current XML buffer (text value of node and sub-node will be appended to this buffer)
in nIndentValue
current indentation value for the XML node
in nIndentStep
indentation step for every XML node (0 for none)

Return value : HRESULT = S_OK for success, otherwise an error occured

Description : This function call itself (recursive).

    static HRESULT MakeXMLBuffer(IXMLDOMNode *pParentNode, CString &szXMLBuffer, int nIndentValue, int nIndentStep);

Back to the top of CXMLTool

HRESULT CreateXMLNode(IXMLDOMDocument* pDoc, DOMNodeType type, BSTR bstrName, IXMLDOMNode **node);

Purpose: create a node in an XML Document tree

Parameters:

in/out pDoc
document tree to update
in type
type of the node (NODE_ELEMENT, NODE_ATTRIBUTE, NODE_TEXT, NODE_CDATA_SECTION, NODE_ENTITY_REFERENCE, NODE_ENTITY, NODE_PROCESSING_INSTRUCTION, NODE_COMMENT, NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_DOCUMENT_FRAGMENT or NODE_NOTATION).
in bstrName
name of the node
out node
pointer to the created node

Return value : HRESULT = S_OK for success

Description :

    static HRESULT CreateXMLNode(IXMLDOMDocument* pDoc, DOMNodeType type, BSTR bstrName, IXMLDOMNode **node);

Back to the top of CXMLTool

HRESULT DeleteXMLNode(IXMLDOMNode *pNode);

Purpose: delete a node

Parameters:

in/out pNode
pointer to the node to be deleted

Return value : HRESULT = S_OK for success

Description :

    static HRESULT DeleteXMLNode(IXMLDOMNode *pNode);

Back to the top of CXMLTool

HRESULT CreateXMLNodeChild(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, IXMLDOMNode **pNode);

Purpose: create a node beneath (child of) an existing node

Parameters:

in/out pDoc
document tree where the node will be created
in type
type of the node (see CreateXMLNode() for possible values)
in lpszName
name of the node to create
in pParentNode
pointer to the parent of the created node
out pNode
pointer to the created node

Return value : HRESULT = S_OK for success

Description : calls

    static HRESULT CreateXMLNodeChild(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, IXMLDOMNode **pNode);

Back to the top of CXMLTool

HRESULT CreateXMLNodeChildText(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, LPCTSTR lpszNodeText, IXMLDOMNode **pNode);

Purpose: create a TEXT node beneath (child of) an existing node

Parameters:

in/out pDoc
document tree where the node will be created
in type
type of the node (see CreateXMLNode() for possible values)
in lpszName
name of the node to create
in lpszNodeText
text of the node to create
in pParentNode
pointer to the parent of the created node
out pNode
pointer to the created node

Return value : HRESULT = S_OK for success

Description : calls CreateXMLNodeChild() twice: once for the node, once for the TEXT (which is a child node of type TEXT).

    static HRESULT CreateXMLNodeChildText(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, LPCTSTR lpszNodeText, IXMLDOMNode **pNode);

Back to the top of CXMLTool

BOOL LoadXMLDocumentFromMemory(IXMLDOMDocument *pXMLDOMDocument, BSTR pBXMLText, char *lpszErrorMessage);

Purpose: load an XML document from a buffer in memory

Parameters:

in/out pXMLDOMDocument
pointer to the loaded document
in pBXMLText
pointer to the document buffer
out lpszErrorMessage
error message (if any)

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMDocument::loadXML()

    static BOOL LoadXMLDocumentFromMemory(IXMLDOMDocument *pXMLDOMDocument, BSTR pBXMLText, char *lpszErrorMessage);

Back to the top of CXMLTool

BOOL CheckTagPresence(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName);

Purpose: check if a tag is present in an XML document

Parameters:

in pXMLDOMDocument
XML document to search
in lpszTagName
name of the tag to look for

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMDocument::getElementsByTagName() and IXMLDOMNodeList::get_length().

    static BOOL CheckTagPresence(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName);

Back to the top of CXMLTool

BOOL GetTagText(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName, CString &szTagText);

Purpose: returns the text within a tag

Parameters:

in pXMLDOMDocument
XML document to search
in lpszTagName
name of the tag
out szTagText
text within the tag

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMDocument::getElementsByTagName() and IXMLDOMNode::get_text().

If several nodes have the given name, the text of the first node is returned.

    static BOOL GetTagText(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName, CString &szTagText);

Back to the top of CXMLTool

BOOL GetXMLParseError(IXMLDOMDocument *pXMLDOMDocument, CString &szErrorReason, long &nErrorCode, long &nLineNumber);

Purpose: returns an XML parsing error

Parameters:

in pXMLDOMDocument
XML document where the error occured
out szErrorReason
reason for error
out nErrorCode
code of error (same as a WIN32 HRESULT)
out nLineNumber
line number where error occured

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMDocument::get_parseError() and then IXMLDOMParseError::get_errorCode(), IXMLDOMParseError::get_reason(), IXMLDOMParseError::get_line()

    static BOOL GetXMLParseError(IXMLDOMDocument *pXMLDOMDocument, CString &szErrorReason, long &nErrorCode, long &nLineNumber);

Back to the top of CXMLTool

BOOL GetChildTagText(IXMLDOMNode *pParentNode, LPCTSTR lpszTagName, CString &szTagText);

Purpose: get text within a tag beneath (child of) a given node

Parameters:

in pParentNode
pointer to the parent node
in lpszTagName
name of the child name
out szTagText
text within the named child

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMNode::selectSingleNode() and IXMLDOMNode::get_text()

    static BOOL GetChildTagText(IXMLDOMNode *pParentNode, LPCTSTR lpszTagName, CString &szTagText);

Back to the top of CXMLTool

BOOL GetTopLevelNode(IXMLDOMDocument *pXMDOMDocument, IXMLDOMNode **ppTopLevelNode);

Purpose: returns the top-level node of an XML document

Parameters:

in pXMDOMDocument
document to examine
out ppTopLevelNode
pointer to the top-level node of the document

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMDocument::get_documentElement() and QueryInterface(IID_IXMLDOMNode, ...).

    static BOOL GetTopLevelNode(IXMLDOMDocument *pXMDOMDocument, IXMLDOMNode **ppTopLevelNode);

Back to the top of CXMLTool

BOOL CopyNode(IXMLDOMNode *pSourceNode, IXMLDOMNode *pDestinationNode, IXMLDOMDocument *pXMLDOMDocument);

Purpose: Copy (recursively) a node

Parameters:

in pSourceNode
source node (complete tree)
in pDestinationNode
destination node
in/out pXMLDOMDocument
XML document to be modified (both nodes must be within this document)

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMNode::cloneNode() to copy node (recursive) and call IXMLDOMNode::appendChild to put the cloned tree underneath destination node.

    static BOOL CopyNode(IXMLDOMNode *pSourceNode, IXMLDOMNode *pDestinationNode, IXMLDOMDocument *pXMLDOMDocument);

Back to the top of CXMLTool

BOOL GetFirstNodeWithName(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszNodeName, IXMLDOMNode **ppNode);

Purpose: returns the first node with a given name

Parameters:

in pXMLDOMDocument
XML document to search
in lpszNodeName
name of the node to look for
out ppNode
found node (if any)

Return value : BOOL = TRUE for success, FALSE otherwise

Description : calls IXMLDOMDocument::getElementsByTagName() and picks the first element in the returned list.

    static BOOL GetFirstNodeWithName(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszNodeName, IXMLDOMNode **ppNode);

Back to the top of CXMLTool

CString GetTagName(IXMLDOMNode *pNode);

Purpose: return the name of the tag in a node

Parameters:

in pNode
pointer to the node

Return value : CString = name of the node

Description : calls IXMLDOMNode::get_nodeName()

    static CString GetTagName(IXMLDOMNode *pNode);

Back to the top of CXMLTool

CString GetXMLPath(IXMLDOMNode *pNode);

Purpose: return the full path of an XML node

Parameters:

in pNode
pointer to the node

Return value : CString = fully qualified name of the node

Description : calls IXMLDOMNode::get_nodeName() on every parent node (up to the top-level node) and build the fully qualified name.

    static CString GetXMLPath(IXMLDOMNode *pNode);

Back to the top of CXMLTool

All Members

public:
static HRESULT MakeXMLBuffer(IXMLDOMDocument *pXMLDOMDocument, CString &szXMLBuffer, int nIndentStep);
static HRESULT MakeXMLBuffer(IXMLDOMNode *pParentNode, CString &szXMLBuffer, int nIndentValue, int nIndentStep);
static HRESULT CreateXMLNode(IXMLDOMDocument* pDoc, DOMNodeType type, BSTR bstrName, IXMLDOMNode **node);
static HRESULT DeleteXMLNode(IXMLDOMNode *pNode);
static HRESULT CreateXMLNodeChild(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, IXMLDOMNode **pNode);
static HRESULT CreateXMLNodeChildText(IXMLDOMDocument* pDoc, DOMNodeType type, LPCTSTR lpszName, IXMLDOMNode *pParentNode, LPCTSTR lpszNodeText, IXMLDOMNode **pNode);
static BOOL LoadXMLDocumentFromMemory(IXMLDOMDocument *pXMLDOMDocument, BSTR pBXMLText, char *lpszErrorMessage);
static BOOL CheckTagPresence(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName);
static BOOL GetTagText(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszTagName, CString &szTagText);
static BOOL GetXMLParseError(IXMLDOMDocument *pXMLDOMDocument, CString &szErrorReason, long &nErrorCode, long &nLineNumber);
static BOOL GetChildTagText(IXMLDOMNode *pParentNode, LPCTSTR lpszTagName, CString &szTagText);
static BOOL GetTopLevelNode(IXMLDOMDocument *pXMDOMDocument, IXMLDOMNode **ppTopLevelNode);
static BOOL CopyNode(IXMLDOMNode *pSourceNode, IXMLDOMNode *pDestinationNode, IXMLDOMDocument *pXMLDOMDocument);
static BOOL GetFirstNodeWithName(IXMLDOMDocument *pXMLDOMDocument, LPCTSTR lpszNodeName, IXMLDOMNode **ppNode);
static CString GetTagName(IXMLDOMNode *pNode);
static CString GetXMLPath(IXMLDOMNode *pNode);
protected:

Back to the top of CXMLTool

Ancestors

Class does not inherit from any other class.

Back to the top of CXMLTool

Descendants

Class is not inherited by any others.

Back to the top of CXMLTool

Generated from source by the Cocoon utilities on Tue Feb 01 17:33:24 2000 .

Report problems to jkotula@stratasys.com