Hi, I'm trying to make a simple xml reader that is using xerces. It can only read the data from xml tags and attributes. However I'm currently confused. Anyway this is my whole header so far:
Code:
// INCLUDES
...
//FORWARD DECLARATION
class CXmlReader;
/**
* @todo: add doxygen comments
*
*/
class CXmlAttribute
{
public:
////////////////////////////////////////////////////////////////////
// ENUM
/**
* @todo: add doxygen comments
*/
enum CXmlAttributeType
{
XML_ATTRIB_STRING,
XML_ATTRIB_BOOL,
XML_ATTRIB_INT,
XML_ATTRIB_FLOAT,
XML_ATTRIB_VEC2F,
XML_ATTRIB_VEC3F,
XML_ATTRIB_VEC4F
};
////////////////////////////////////////////////////////////////////
// METHODS
/**
* Get the Element Attribute
* @todo: add more explanation
*/
std::string GetElementAttribute( xercesc::DOMElement& element
, const std::string& attribName
);
/**
* Get the Element Attribute as a string
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, std::string& result
, const std::string& defValue = ""
);
/**
* Get the Element Attribute as a boolean
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, bool& result
, bool defValue = false
);
/**
* Get the Element Attribute as a float
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, float& result
, float defValue = 0.0f
);
/**
* Get the Element Attribute as an integer
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, int& result
, int defValue = 0
);
/**
* Get the Element Attribute as a vector 2f
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, CVec2f& result
, const CVec2f& defValue = CVec2f()
);
/**
* Get the Element Attribute as a vector 3f
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, CVec3f& result
, const CVec3f& defValue = CVec3f()
);
/**
* Get the Element Attribute as a vector 4f
* @todo: add more explanation
*/
bool GetAttribute( xercesc::DOMElement& element
, const std::string& attribName
, CVec4f& result
, const CVec4f& defValue = CVec4f()
);
private:
};
/**
* @todo: add doxygen comments
*
*/
class CXmlTag
{
public:
friend class CXmlReader;
////////////////////////////////////////////////////////////////////
// STRUCTS
/**
* @todo: add doxygen comments
*/
struct sAttribIdentifier
{
std::string name; ///< The name of the attribute
CXmlAttribute::CXmlAttributeType type; ///< The type of the attribute
};
////////////////////////////////////////////////////////////////////
// METHODS
////////////////////////////////////////////////////////////////
// Constructor / Destructor
/**
* @todo: add doxygen comments
*/
CXmlTag(): mTagName("")
{
mAttribIdentifiers.clear();
mChildTags.clear();
mMemberAttribs.clear();
}
/**
* @todo: add doxygen comments
*/
CXmlTag(const std::string& tagName): mTagName(tagName)
{
mAttribIdentifiers.clear();
mChildTags.clear();
mMemberAttribs.clear();
}
/**
* @todo: add doxygen comments
*/
~CXmlTag()
{
mAttribIdentifiers.clear();
mChildTags.clear();
mMemberAttribs.clear();
}
////////////////////////////////////////////////////////////////
// Generic Methods
/**
* @todo: add doxygen comments
*/
bool ParseTagElement(xercesc::DOMElement *elem);
/**
* @todo: add doxygen comments
*/
void AddAttribIdentifier(sAttribIdentifier attribIdent);
/**
* @todo: add doxygen comments
*/
void SetTagName(const std::string& tagName);
/**
* @todo: add doxygen comments
*/
const std::string GetTagName() const;
protected:
////////////////////////////////////////////////////////////////////
// VARIABLES
std::string mTagName; ///< The name / label of the tag
std::vector<sAttribIdentifier> mAttribIdentifiers; ///< The vector that holds the type and name of the attributes that will be extracted from this tag. This should be unique.
std::vector<CXmlTag> mChildTags; ///< The vector that holds the other tags that has become this tag's children
std::map<sAttribIdentifier, CXmlAttribute> mAttributes; ///< The vector that holds the attributes data of this tag
private:
};
/**
* @todo: add doxygen comments
*
*/
class CXmlReader
{
public:
////////////////////////////////////////////////////////////////
// CONSTANTS
////////////////////////////////////////////////////////////////////
// METHODS
////////////////////////////////////////////////////////////////
// Constructor / Destructor
/**
* @todo: add doxygen comments
*/
CXmlReader();
/**
* @todo: add doxygen comments
*/
~CXmlReader();
////////////////////////////////////////////////////////////////
// Generic methods
/**
* @todo: add doxygen comments
*/
bool LoadXML( const std::string& fileName
, const std::string& rootTagName = ""
, bool isCriticalError = false
);
private:
////////////////////////////////////////////////////////////////////
// METHODS
/**
* Parsing XML file
* @todo: add doxygen comments
*/
virtual bool ParseXML( const std::string& fileName
, const std::string& rootTagName
, bool isCriticalError
);
protected:
////////////////////////////////////////////////////////////////////
// VARIABLES
dtUtil::Log& mLogger; ///< The logger object
std::string mXmlFilePathName; ///< Full path filename of the xml file
std::string mRootTagName; ///< The tag name (or label?) of the root (default: root)
bool mIsCriticalError; ///< Is the XML critical or not? If true, throw critical exception on error. If not, just log the error.
xercesc::XercesDOMParser mXmlParser; ///< Xerces parser
xercesc::DOMDocument *mXmlDoc; ///< Xerces DOM document
xercesc::DOMElement *mXmlRootDomElement; ///< The first Xerces DOM element (root element)
CXmlTag mRootXmlTagData; ///< The root data extracted from the XML.
};
As you can see in the CXmlAttribute class, there's no member variable at all. This is because I'm confused as what variable to use. FYI, the value of the attribute can be of any type that is enumerated with eXmlAttributeType. It is actually being read as a string from xerces so my initial plan is to store the string value and just translate it into the correct type using one of the GetAttribute() method. But that is not really efficient and I better store it into the correct type. So any suggestion of what I should use? Templates? Void pointers? Thanks in advance.