I want to wrap part of the TinyXML library in some custom classes in my project because I only need some of its functionality and I do not wish to expose everything.
I have a problem where my XMLDocument::AddNode(...)
function is basically doing the same thing that my XMLNode
class is meant for. I wondered if anyone could give me some design tips on how to wrap the TinyXML library so I don't break encapsulation and my wrapper classes don't expose the TinyXML library to the rest of my code.
class XMLNode
{
public:
XMLNode::XMLNode();
XMLNode::XMLNode(const std::string& element); // Creates a TiXMLElement object
XMLNode::XMLNode(XMLNode& nodycopy);
XMLNode::~XMLNode();
XMLNode GetFirstChild();
XMLNode GetNextSibling();
std::string GetNodeValue();
std::string GetNodeName();
void AddNodeText(const std::string& text);
void AddNodeAttribute();
private:
std::string value;
std::string nodename;
TiXmlElement* thisnode;
};
//These functions do the same as my AddNode does from XMLDocument, it seems rather silly...
XMLNode::XMLNode(const std::string& str_element)
{
thisnode = new TiXmlElement(str_element.c_str());
nodename = str_element;
}
void XMLNode::AddNodeText(const std::string& nodetext)
{
TiXmlText* text = new TiXmlText(nodetext.c_str());
thisnode->LinkEndChild(text);
value = nodetext;
}
class XMLDocument
{
public:
XMLDocument::XMLDocument(const std::string& documentname);
XMLDocument::~XMLDocument();
void SaveToFile(std::string filename);
std::string basicXML(std::string rootnode);
void AddNode(XMLNode& node);
XMLNode GetXPathNode(const std::string& node);
void AppendCloneNodeAsChild(XMLNode& nodetoappend);
void SetRoot(XMLNode& rootnode);
private:
TiXmlDocument document;
TiXmlElement root;
TiXmlElement currentelement;
};
void XMLDocument::AddNode(XMLNode& node) // This function does over what XMLNode class is actually for... I just want to add the node to the XMLDocument
{
std::string val = node.GetNodeName();
TiXmlElement* el = new TiXmlElement(val.c_str());
TiXmlText * txt = new TiXmlText(node.GetNodeValue().c_str());
el->LinkEndChild(txt);
document.LinkEndChild(el);
}
Can anybody give me some suggestions on how to wrap this properly and only expose the functionality of TinyXML that I want?