ADTF  3.18.2
DOMElement

Representation for an element in the DOM. More...

Public Member Functions

 DOMElement ()
 CTOR.
 
 ~DOMElement ()
 DTOR.
 
 DOMElement (const DOMElement &element)
 Copy constructor. More...
 
DOMElementoperator= (const DOMElement &element)
 Copy assignment operator. More...
 
std::string getAttribute (const std::string &name, const std::string &def_value=std::string()) const
 Returns the value of an attribute with name. More...
 
DOMAttributes getAttributes () const
 Get a map of all attributes of this element. More...
 
DOMElement getChild (const std::string &name) const
 Get the first child mathing the name of this element. More...
 
DOMElementList getChildren () const
 Get a list of all child elements of the element. More...
 
std::string getData () const
 Get the data of the element. More...
 
DOM getDocument () const
 Get the DOM document this element belongs to. More...
 
std::string getName () const
 Get the name of the element. More...
 
DOMElement getParent () const
 Get this elements parent element. More...
 
std::string getPath () const
 Get current path of the element. More...
 
bool setAttribute (const std::string &name, const std::string &strValue)
 Sets an attribute. More...
 
bool setData (const std::string &data)
 Sets the data of the element. More...
 
bool setName (const std::string &name)
 Sets the name of the element. More...
 
bool hasAttribute (const std::string &name) const
 Checks if an attribute exists. More...
 
bool eraseAttribute (const std::string &name)
 Removes an attribute. More...
 
bool findNode (const std::string &query, DOMElement &element_ptr) const
 Finds a node based on a query. More...
 
bool findNodes (const std::string &query, DOMElementList &elements) const
 Finds nodes based on a query. More...
 
bool sortNodes (const std::string &query, SortingOrder order)
 Sort the queried nodes by name example: More...
 
bool sortNodes (const std::string &query, SortingOrder order, std::size_t number_of_nodes)
 Sort all queried nodes by name example: More...
 
std::size_t sortNodes (const std::string &query, const std::string &attribute, SortingOrder order)
 Sort the queried nodes by attribute example: More...
 
bool sortNodes (const std::string &query, const std::string &attribute, SortingOrder order, std::size_t number_of_nodes)
 Sort all queried nodes by attribute example: More...
 
DOMElement createChild (const std::string &name)
 Creates a new child element. More...
 
DOMElement createChildBefore (const std::string &name, const DOMElement &before)
 Creates a new child element before an existing child element. More...
 
bool removeChild (const std::string &name)
 Erases the first child of that name in this element. More...
 
bool isNull () const
 Check whether this DOMElement is empty. More...
 

Private Attributes

memory::StackPtr< Implementation, 16 > _impl
 

Detailed Description

Constructor & Destructor Documentation

◆ DOMElement()

DOMElement ( const DOMElement element)

Copy constructor.

Parameters
[in]elementThe object to copy from

Member Function Documentation

◆ createChild()

DOMElement createChild ( const std::string &  name)

Creates a new child element.

Parameters
[in]nameThe name of the child
Returns
The new child

◆ createChildBefore()

DOMElement createChildBefore ( const std::string &  name,
const DOMElement before 
)

Creates a new child element before an existing child element.

Parameters
[in]nameThe name of the child
[in]beforeThe existing child element
Returns
The new child

◆ eraseAttribute()

bool eraseAttribute ( const std::string &  name)

Removes an attribute.

Parameters
[in]nameThe name of the attribute
Returns
true if the attribute was erased, false otherwise

◆ findNode()

bool findNode ( const std::string &  query,
DOMElement element_ptr 
) const

Finds a node based on a query.

Some basic syntax:

  • queries starting with '/' are starting at the root node
  • use '*' to search all child nodes in the path
  • if you search with absolute paths, don't forget to use the root node in the path.
  • to search for certain attributes on nodes use [@property0='value'] or [@property0] to check the existence. Keep in mind that attributes search method will include parent (root) node in the elements list if search value match. In this case parent node will be the first element in the list.
  • attributes can be concatenated to make them behave like an AND: [@property0][@property1='value']
  • recursive search from root starts with '//nodes_to_search'. The whole tree is being searched.
  • recursive search from current node starts with './/nodes_to_search'. The whole subtree is being searched.
  • use 'and' or 'or' to connect multiple properties logically (USE ONLY ONE OPERATOR!)

Here are some examples of search queries.

  • /root/path/to/node :absolute path to nodes
  • path/to/node :search starting on current node. Same as above, if searched in root node.
  • /root/node/* :find all children of all 'node' nodes
  • * :find all children of current node
  • /root/node[@prop='2']/* :find all children of 'node' nodes with matching attribute
  • //node :finds all 'node' nodes in the tree (recursive search)
  • .//node :recursive search starting from current node
  • //*[@prop='2'] :find all nodes in the tree with matching attributes
  • /root/path/to[@prop='2']//node :find all 'node' nodes in the subtree of the sub paths '/root/path/to'
  • child/*[@prop1='2'] :find all nodes in 'child' nodes with matching attributes
  • *[@prop1='3' and @prop2='4'] :logical AND comparison for attributes
Parameters
[in]queryThe query string
[out]element_ptrThis will point to the found element
Returns
false if no matching node is found or the query is invalid, true otherwise

◆ findNodes()

bool findNodes ( const std::string &  query,
DOMElementList elements 
) const

Finds nodes based on a query.

See findNode() for the syntax.

Parameters
[in]queryThe query string
[out]elementsThis list will be filled with the found elements
Returns
false if no matching node is found or the query is invalid, true otherwise

◆ getAttribute()

std::string getAttribute ( const std::string &  name,
const std::string &  def_value = std::string() 
) const

Returns the value of an attribute with name.

Parameters
[in]nameThe name of the attribute
[in]def_valueA default value that is returned if the attribute does not exist
Returns
The value of the attribute or the default value

◆ getAttributes()

DOMAttributes getAttributes ( ) const

Get a map of all attributes of this element.

Returns
A map of all attributes of the element

◆ getChild()

DOMElement getChild ( const std::string &  name) const

Get the first child mathing the name of this element.

Parameters
[in]nameThe name of the child element
Returns
The child element or a nullptr if not found

◆ getChildren()

DOMElementList getChildren ( ) const

Get a list of all child elements of the element.

Returns
The element list of all children

◆ getData()

std::string getData ( ) const

Get the data of the element.

Returns
The data

◆ getDocument()

DOM getDocument ( ) const

Get the DOM document this element belongs to.

Returns
The document this element belongs to

◆ getName()

std::string getName ( ) const

Get the name of the element.

Returns
The name of the element

◆ getParent()

DOMElement getParent ( ) const

Get this elements parent element.

Returns
The parent element - empty if no parent element exists

◆ getPath()

std::string getPath ( ) const

Get current path of the element.

Returns
Returns the path

◆ hasAttribute()

bool hasAttribute ( const std::string &  name) const

Checks if an attribute exists.

Parameters
[in]nameThe name of the attribute
Returns
true if attribute exists, otherwise false

◆ isNull()

bool isNull ( ) const

Check whether this DOMElement is empty.

Returns
true if empty, false otherwise

◆ operator=()

DOMElement& operator= ( const DOMElement element)

Copy assignment operator.

Parameters
[in]elementThe element to copy from
Returns
*this

◆ removeChild()

bool removeChild ( const std::string &  name)

Erases the first child of that name in this element.

Parameters
name[in] The name of the child element
Returns
true if the child was removed, false otherwise

◆ setAttribute()

bool setAttribute ( const std::string &  name,
const std::string &  strValue 
)

Sets an attribute.

Setting an attribute on an DOMElement object not created from an existing DOM will fail as a DOMElement needs to be linked to a valid DOM

Parameters
[in]nameThe attribute name
[in]strValueThe value of the attribute
Returns
false if its an invalid attribute name, true otherwise

◆ setData()

bool setData ( const std::string &  data)

Sets the data of the element.

Setting the data on an DOMElement object not created from an existing DOM will fail as a DOMElement needs to be linked to a valid DOM

Parameters
[in]dataThe new data
Returns
true if the data was set, false otherwise

◆ setName()

bool setName ( const std::string &  name)

Sets the name of the element.

Setting the name on an DOMElement object not created from an existing DOM will fail as a DOMElement needs to be linked to a valid DOM

Parameters
[in]nameThe new name
Returns
true if the name was set, false otherwise.

◆ sortNodes() [1/4]

std::size_t sortNodes ( const std::string &  query,
const std::string &  attribute,
SortingOrder  order 
)

Sort the queried nodes by attribute example:

sortNodes("units/unit", "name", SortingOrder::ascending);
bool sortNodes(const std::string &query, SortingOrder order)
Sort the queried nodes by name example:
@ ascending
Sort in ascending order.
Parameters
[in]queryXpath that describes the nodes to sort
[in]attributeAttribute name of nodes to sort in specified order
[in]orderSort nodes in ascending or descending order
Returns
Number of nodes found by the query

◆ sortNodes() [2/4]

bool sortNodes ( const std::string &  query,
const std::string &  attribute,
SortingOrder  order,
std::size_t  number_of_nodes 
)

Sort all queried nodes by attribute example:

sortNodes("units/unit[*]/refUnit", "name", SortingOrder::ascending, number_of_nodes);

'[*]' in query will be replaced successively by numbers in range [1]..[n]

Parameters
[in]queryXpath that describes all nodes to sort
[in]attributeAttribute name of nodes to sort in specified order
[in]orderSort nodes of queries in ascending/descending order
[in]number_of_nodesNumber of nodes to be sorted successively
Returns
false if no matching nodes are found or any query is invalid, true otherwise

◆ sortNodes() [3/4]

bool sortNodes ( const std::string &  query,
SortingOrder  order 
)

Sort the queried nodes by name example:

Parameters
[in]queryXpath that describes the nodes to sort
[in]orderSort nodes in ascending or descending order
Returns
false if no matching node is found or the query is invalid, true otherwise

◆ sortNodes() [4/4]

bool sortNodes ( const std::string &  query,
SortingOrder  order,
std::size_t  number_of_nodes 
)

Sort all queried nodes by name example:

sortNodes("units/unit[*]/*", SortingOrder::ascending, number_of_nodes);

'[*]' in query will be replaced successively by numbers in range [1]..[n]

Parameters
[in]queryXpath that describes all nodes to sort
[in]orderSort nodes of queries in ascending/descending order
[in]number_of_nodesNumber of nodes to be sorted successively
Returns
false if no matching nodes are found or any query is invalid, true otherwise