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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.