UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
util::xml::IXmlNode Class Reference

Interface class against a XML tag (node) in a XML file. More...

#include <util/ixmlnode.h>

Public Types

using ChildList = std::vector<const IXmlNode *>
 List of pointer to child nodes.

Public Member Functions

 IXmlNode (const std::string &tag_name)
 Creates a XML tag.
virtual ~IXmlNode ()=default
 Destructor.
std::string TagName () const
 Returns the nodes tag name.
void TagName (const std::string &name)
 Sets the tag name.
bool IsTagName (const std::string &tag) const
 Returns true if the tag name match. Check if this is a specific tag name. The check ignore case and namespaces.
bool IsAttribute (const std::string &key, const std::string &value) const
 Checks if an attribute exist.
template<typename T>
Attribute (const std::string &key, const T &def={}) const
 Returns an attribute.
template<typename T>
void SetAttribute (const std::string &key, const T &value)
 Sets an attribute.
template<typename T>
Value () const
 Returns a value.
template<typename T>
void Value (const T &value)
 Sets a tag value.
template<typename T>
Property (const std::string &key, const T &def={}) const
 Returns a specific tag value.
template<typename T>
void SetProperty (const std::string &key, const T &value)
 Sets a property.
virtual void GetChildList (ChildList &child_list) const
 Returns the child nodes.
virtual const IXmlNodeGetNode (const std::string &tag) const
 Returns a node if it exist.
virtual const IXmlNodeGetNode (const std::string &tag, const std::string &key, const std::string &attr) const
 Returns a node with a specific attribute.
bool ExistProperty (const std::string &tag) const
 Returns true if the named property exist.
virtual IXmlNodeAddNode (const std::string &name)
 Adds a new tag.
virtual IXmlNodeAddUniqueNode (const std::string &name)
 Adds a unique tag.
virtual IXmlNodeAddUniqueNode (const std::string &name, const std::string &key, const std::string &attr)
 Adds a unique tag with an attribute.
virtual void AddNode (std::unique_ptr< IXmlNode > p)
 Adds a node.
void DeleteNode (const std::string &name)
 Deletes all nodes with name.
void DeleteNode (const IXmlNode *node)
 Deletes a specific node.
virtual void Write (std::ostream &dest, size_t level)
 Write the node to the stream.
bool HasChildren () const
 Returns true if the XML node has children.
template<>
void Value (const bool &value)
 Adds a boolean value.
template<>
void Value (const std::string &value)
 Adds a string value.
template<>
void SetAttribute (const std::string &key, const bool &value)
 Sets a boolean attribute.

Protected Types

using AttributeList = std::unordered_map<std::string, std::string>
 Indexed list of to key, attribute value pair.
using NodeList = std::vector<std::unique_ptr<IXmlNode> >
 List of nodes.

Protected Member Functions

virtual std::unique_ptr< IXmlNodeCreateNode (const std::string &name) const
 Create a node.

Protected Attributes

std::string tag_name_
 Name of this tag.
std::string value_
 String value of this tag.
AttributeList attribute_list_
 Indexed list of attribute.
NodeList node_list_
 List of nodes.

Detailed Description

]

The class is the main interface when an XML file is more than one level deep. Simple configuration files normally only have a root tag including one or more value tags (properties).

Each tag have a tag name and none or more attributes. If the tag name is unique within the file, there is no need for attributes but if many tags with the same name, it's recommended to set a unique attribute on each tag.

Member Function Documentation

◆ Attribute()

template<typename T>
T util::xml::IXmlNode::Attribute ( const std::string & key,
const T & def = {} ) const
inlinenodiscard

Returns an attribute

Template Parameters
TType of value. Can be string, numbers or boolean.
Parameters
[in]keyAttribute name.
[in]defDefault value if attribute is missing.
Returns
Returns the attribute value or default value..

◆ ExistProperty()

bool util::xml::IXmlNode::ExistProperty ( const std::string & tag) const
inlinenodiscard

Returns true if the named property exists. This function is normally used when deciding if an attribute or a property key/value should be used.

Parameters
tagThe name of the key (tag).
Returns
True if it exist

◆ HasChildren()

bool util::xml::IXmlNode::HasChildren ( ) const
inlinenodiscard
Returns
True if the XML node has children.

◆ IsAttribute()

bool util::xml::IXmlNode::IsAttribute ( const std::string & key,
const std::string & value ) const
nodiscard

Tests if an attribute exist.

Parameters
keyAttribute name
valueAttribute vallue
Returns

◆ IsTagName()

bool util::xml::IXmlNode::IsTagName ( const std::string & tag) const
nodiscard

Example: If Tag name is '<ns:Foo>', IsTagName("foo") -> true

Parameters
[in]tagTag name
Returns
True if the tag name match.

◆ Property()

template<typename T>
T util::xml::IXmlNode::Property ( const std::string & key,
const T & def = {} ) const
inlinenodiscard

Return a child tag value. Note that the Value() function returns this tags value.

Template Parameters
TType of value. Can be string, numbers or boolean.
Parameters
[in]keyChild tag name.
[in]defDefault value.
Returns
The child tags value.

◆ SetAttribute() [1/2]

template<>
void util::xml::IXmlNode::SetAttribute ( const std::string & key,
const bool & value )
Parameters
keyAttribute name
valueAttribute boolean value

◆ SetAttribute() [2/2]

template<typename T>
void util::xml::IXmlNode::SetAttribute ( const std::string & key,
const T & value )
Template Parameters
TType of value
Parameters
keyAttribute name
valueAttribute value

◆ SetProperty()

template<typename T>
void util::xml::IXmlNode::SetProperty ( const std::string & key,
const T & value )
inline

Sets a property

Template Parameters
TType of value
Parameters
keyTag name
valueValue.

◆ TagName() [1/2]

std::string util::xml::IXmlNode::TagName ( ) const
inlinenodiscard

Return the XML nodes tag name.

Returns
Teturns the tag name.

◆ TagName() [2/2]

void util::xml::IXmlNode::TagName ( const std::string & name)
inline

Sets the node tag name.

Parameters
nameThe new tag name.

◆ Value() [1/4]

template<typename T>
T util::xml::IXmlNode::Value ( ) const
inlinenodiscard

Returns the tag value

Template Parameters
TType of value. Can be string, numbers or boolean.
Returns
Returns the tag value

◆ Value() [2/4]

template<>
void util::xml::IXmlNode::Value ( const bool & value)
Parameters
valueBoolean value

◆ Value() [3/4]

template<>
void util::xml::IXmlNode::Value ( const std::string & value)
Parameters
valueString value

◆ Value() [4/4]

template<typename T>
void util::xml::IXmlNode::Value ( const T & value)

Sets a tag value.

Template Parameters
TType of value
Parameters
valueValue

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