Click here to Skip to main content
15,888,005 members
Articles / Desktop Programming / MFC
Article

Wrapper class for IXMLDDOMDocument

Rate me:
Please Sign up or sign in to vote.
4.67/5 (10 votes)
10 Apr 20042 min read 89.9K   1K   29   25
A class that provides easy access to XML documents.

Introduction

This simple class provides a few easy methods to work with XML files, including loading XML, building XML hierarchy and saving back to XML file, navigating the nodes and a simple search method.

Classes

CXMLDocument

Methods
HRESULT Load(LPCTSTR fileName); 
HRESULT Save(LPCTSTR fileName);

Loads and saves the XML document.

Members
CXMLNode Root;

The root node of the document.

CXMLNode

Methods
void Find(CXMLNode* rootNode, LPCTSTR nodeName = 
    "*",LPCTSTR nodeText = "*",LPCTSTR attribName = "*",
    LPCTSTR attribValue = "*");
CXMLAttribute* GetAttribute(LPCTSTR attribName);
void AddAttribute(LPCTSTR attribName,LPCTSTR attribValue);
void AddAttribute(LPCTSTR attribName,int attribValue);
void AddAttribute(LPCTSTR attribName,short attribValue);
void AddAttribute(LPCTSTR attribName,double attribValue);
void AddAttribute(LPCTSTR attribName,float attribValue);
void AddAttribute(LPCTSTR attribName,bool attribValue);
void Clear();
CXMLNode* AddNode(LPCTSTR nodeName, LPCTSTR nodeText);
Members
CArray <CXMLNode*,CXMLNode*> Nodes;
CArray <CXMLAttribute*,CXMLAttribute*> Attributes;
CString Name;
CString Text;
Methods usage

CXMLNode* AddNode(LPCTSTR nodeName, LPCTSTR nodeText);

Adds a new node to the array of nodes and returns pointer to this node.

  • nodeName - the new node will be created with this name.
  • nodeText - this will be the text value for the new node.

void AddAttribute(LPCTSTR attribName,LPCTSTR attribValue); + 5 ovverloads.

Adds a new attribute to this node.

  • attribName - the name of the attribute.
  • attribValue - the value of the attribute.

CXMLAttribute* GetAttribute(LPCTSTR attribName);

Finds an attribute by name, returns a pointer to that attribute, NULL if not found.

  • attribName - the name of the attribute to return.

void Clear();

Clears this node of ALL its children, recursively clears the children as well, and clears the attributes also.

void Find(CXMLNode* rootNode, LPCTSTR nodeName = "*",LPCTSTR nodeText = "*",LPCTSTR attribName = "*",LPCTSTR attribValue = "*");

Recursively searches the children of this node, adding any child node that matches the criteria as a child to the supplied root node.

  • rootNode - pointer to the root node that will hold the search results, must not be NULL.
  • nodeName - the name of the nodes to find or "*" for ANY node.
  • nodeName - the text of the nodes to find or "*" for ANY node.
  • attribName - the name of the attribute to find, nodes that have an attribute with this name pass the test, or "*" for ANY.
  • attribValue - the value of the attribute to find, nodes that have an attribute with this value pass the test, or "*" for ANY.

CXMLAttribute

Methods
bool BoolValue();
double DblValue();
int IntValue();

Those methods are value extractors, returns the stored value as bool, double or integer.

Members
CString Name;
CString Value;

Stores the name and the value of the attribute.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) RDV Systems
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicense? Pin
Member 1229581621-Sep-16 5:26
Member 1229581621-Sep-16 5:26 
GeneralNew version is available Pin
Alex Hazanov2-Apr-05 23:27
Alex Hazanov2-Apr-05 23:27 
GeneralDOM XML Pin
dchauhan27-Mar-05 23:15
dchauhan27-Mar-05 23:15 
GeneralRe: DOM XML Pin
Alex Hazanov27-Mar-05 23:29
Alex Hazanov27-Mar-05 23:29 
QuestionBug in CXMLNode::Find? Pin
Alex Um18-Mar-05 1:16
Alex Um18-Mar-05 1:16 
Questionhow to insert formatting white spaces into output file ? Pin
luk7716-Sep-04 22:17
luk7716-Sep-04 22:17 
AnswerRe: how to insert formatting white spaces into output file ? Pin
Dutchpower18-Sep-04 2:08
Dutchpower18-Sep-04 2:08 
GeneralRe: how to insert formatting white spaces into output file ? Pin
luk7718-Sep-04 14:01
luk7718-Sep-04 14:01 
GeneralRe: how to insert formatting white spaces into output file ? Pin
10-Dec-04 0:35
suss10-Dec-04 0:35 
GeneralRe: how to insert formatting white spaces into output file ? Pin
Alex Hazanov10-Dec-04 6:27
Alex Hazanov10-Dec-04 6:27 
GeneralCan't seem to use in Visual C++ 5.0 Pin
Hydra_427-Apr-04 1:49
Hydra_427-Apr-04 1:49 
GeneralRe: Can't seem to use in Visual C++ 5.0 Pin
Alex Hazanov2-May-04 6:19
Alex Hazanov2-May-04 6:19 
GeneralWon't compile with PlatForm sdk Pin
bkowald19-Apr-04 11:19
bkowald19-Apr-04 11:19 
GeneralRe: Won't compile with PlatForm sdk Pin
Anonymous19-Apr-04 23:28
Anonymous19-Apr-04 23:28 
GeneralThanks Alex Pin
Jed215-Apr-04 14:02
Jed215-Apr-04 14:02 
GeneralNice and clear. Pin
oleg6315-Apr-04 5:17
professionaloleg6315-Apr-04 5:17 
GeneralPut your comments directly in the code! Pin
didier_thizy15-Apr-04 1:55
didier_thizy15-Apr-04 1:55 
GeneralRe: Put your comments directly in the code! Pin
Alex Hazanov15-Apr-04 4:11
Alex Hazanov15-Apr-04 4:11 
GeneralVery nice... Pin
Antony M Kancidrowski13-Apr-04 23:24
Antony M Kancidrowski13-Apr-04 23:24 
GeneralRe: Very nice... Pin
nicoster15-Apr-04 15:56
nicoster15-Apr-04 15:56 
GeneralRe: Very nice... Pin
Antony M Kancidrowski17-Apr-04 9:52
Antony M Kancidrowski17-Apr-04 9:52 
GeneralRe: Very nice... Pin
Alex Hazanov20-Apr-04 4:05
Alex Hazanov20-Apr-04 4:05 
Questionwhat do you want to do with xml? Pin
ChauJohnthan11-Apr-04 2:32
ChauJohnthan11-Apr-04 2:32 
great aritcle indeed, simple, neatSmile | :)

but you dressed a big monkey.
AnswerRe: what do you want to do with xml? Pin
Anna-Jayne Metcalfe12-Apr-04 23:41
Anna-Jayne Metcalfe12-Apr-04 23:41 
AnswerRe: what do you want to do with xml? Pin
Alex Hazanov13-Apr-04 1:16
Alex Hazanov13-Apr-04 1:16 

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

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