iLand
Public Member Functions | List of all members
XmlHelper Class Reference

XmlHelper wraps a XML file and provides some convenient functions to retrieve values. More...

#include <xmlhelper.h>

Public Member Functions

 XmlHelper ()
 
 ~XmlHelper ()
 
 XmlHelper (const QString &fileName)
 
 XmlHelper (QDomElement topNode)
 Create a XmlHelper instance with topNode as top node. More...
 
void loadFromFile (const QString &fileName)
 
QDomElement top () const
 
void setCurrentNode (const QString &path)
 sets path as the current (relative) node. More...
 
void setCurrentNode (const QDomElement &node)
 sets node as the current (relative) top node. More...
 
bool isValid () const
 returns true if the current (relative!) node is valid (i.e. not null). More...
 
bool hasNode (const QString &path) const
 returns true if path exists. More...
 
QDomElement node (const QString &path) const
 retrieve node defined by path (see class description) More...
 
QString value (const QString &path, const QString &defaultValue="") const
 retrieve value (as string) from node path. More...
 
bool valueBool (const QString &path, const bool defaultValue=false) const
 retrieve value (as bool) from node path. More...
 
double valueDouble (const QString &path, const double defaultValue=0.) const
 retrieve value (as double) from node path. More...
 
int valueInt (const QString &path, const int defaultValue=0) const
 retrieve value (as int) from node path. More...
 
bool setNodeValue (QDomElement &node, const QString &value)
 set value of 'node'. return true on success. More...
 
bool setNodeValue (const QString &path, const QString &value)
 set value of node indicated by 'path'. return true on success. More...
 
double paramValue (const QString &paramName, const double defaultValue=0.) const
 get value of special "parameter" space More...
 
QString paramValueString (const QString &paramName, const QString &defaultValue="") const
 get value of special "parameter" space More...
 
bool paramValueBool (const QString &paramName, const bool &defaultValue=true) const
 get value of special "parameter" space More...
 
QStringList dump (const QString &path, int levels=-1)
 

Detailed Description

XmlHelper wraps a XML file and provides some convenient functions to retrieve values.

Internally XmlHelper uses a QDomDocument (the full structure is kept in memory so the size is restricted). Use node() to get a QDomElement or use value() to directly retrieve the node value. Nodes could be addressed relative to a node defined by setCurrentNode() using a ".". The notation is as follows:

QDomElement e,f
e = xml.node("first.second.third"); // e points to "third"
xml.setCurrentNode("first");
f = xml.node(".second.third"); // e and f are equal
e = xml.node("level1[2].level2[3].level3"); // 3rd element of level 1, ...
int x = xml.value(".second", "0").toInt(); // node value of "second" or "0" if not found.
if (xml.valueBool("enabled")) // use of valueBool with default value (false)
...
XmlHelper xml_sec(xml.node("first.second")); // create a xml-helper with top node=first.second
xml_sec.valueDouble("third"); // use this

Constructor & Destructor Documentation

◆ XmlHelper() [1/3]

XmlHelper::XmlHelper ( )

◆ ~XmlHelper()

XmlHelper::~XmlHelper ( )

◆ XmlHelper() [2/3]

XmlHelper::XmlHelper ( const QString &  fileName)
inline

◆ XmlHelper() [3/3]

XmlHelper::XmlHelper ( QDomElement  topNode)

Create a XmlHelper instance with topNode as top node.

The xml tree is not copied.

Member Function Documentation

◆ dump()

QStringList XmlHelper::dump ( const QString &  path,
int  levels = -1 
)

◆ hasNode()

bool XmlHelper::hasNode ( const QString &  path) const

returns true if path exists.

◆ isValid()

bool XmlHelper::isValid ( ) const
inline

returns true if the current (relative!) node is valid (i.e. not null).

◆ loadFromFile()

void XmlHelper::loadFromFile ( const QString &  fileName)

◆ node()

QDomElement XmlHelper::node ( const QString &  path) const

retrieve node defined by path (see class description)

retreives node with given path and a element where isNull() is true if nothing is found.

◆ paramValue()

double XmlHelper::paramValue ( const QString &  paramName,
const double  defaultValue = 0. 
) const

get value of special "parameter" space

numeric values of elements in the section <parameter> are stored in a QHash structure for faster access.

with paramValue() these data can be accessed.

◆ paramValueBool()

bool XmlHelper::paramValueBool ( const QString &  paramName,
const bool &  defaultValue = true 
) const

get value of special "parameter" space

◆ paramValueString()

QString XmlHelper::paramValueString ( const QString &  paramName,
const QString &  defaultValue = "" 
) const

get value of special "parameter" space

◆ setCurrentNode() [1/2]

void XmlHelper::setCurrentNode ( const QDomElement &  node)
inline

sets node as the current (relative) top node.

◆ setCurrentNode() [2/2]

void XmlHelper::setCurrentNode ( const QString &  path)
inline

sets path as the current (relative) node.

◆ setNodeValue() [1/2]

bool XmlHelper::setNodeValue ( const QString &  path,
const QString &  value 
)

set value of node indicated by 'path'. return true on success.

◆ setNodeValue() [2/2]

bool XmlHelper::setNodeValue ( QDomElement &  node,
const QString &  value 
)

set value of 'node'. return true on success.

◆ top()

QDomElement XmlHelper::top ( ) const
inline

◆ value()

QString XmlHelper::value ( const QString &  path,
const QString &  defaultValue = "" 
) const

retrieve value (as string) from node path.

◆ valueBool()

bool XmlHelper::valueBool ( const QString &  path,
const bool  defaultValue = false 
) const

retrieve value (as bool) from node path.

◆ valueDouble()

double XmlHelper::valueDouble ( const QString &  path,
const double  defaultValue = 0. 
) const

retrieve value (as double) from node path.

◆ valueInt()

int XmlHelper::valueInt ( const QString &  path,
const int  defaultValue = 0 
) const

retrieve value (as int) from node path.


The documentation for this class was generated from the following files: