FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
XmlElement Class Reference

Class representing a node in an XML document. More...

#include "libxml_wrapper.h"

Public Member Functions

 XmlElement (const xmlNode *node)
 Constructor for XmlElement.
 
 XmlElement (const XmlElement &)=default
 
 XmlElement (XmlElement &&) noexcept=default
 
XmlElementoperator= (const XmlElement &)=default
 
XmlElementoperator= (XmlElement &&) noexcept=default
 
 ~XmlElement ()=default
 
std::string_view name () const noexcept
 Get the name of the XML element.
 
std::string getText () const
 Get the text content of the XML element.
 
void setText (const std::string_view text) const
 Set the text content of the XML element.
 
void setAttribute (const std::string_view name, const std::string_view value) const
 Set an attribute on the XML element.
 
XmlElement addChild (const std::string_view name) const noexcept
 Add a child element to the current node.
 
XmlElement childElement (const std::string_view name="", const unsigned index=0) const noexcept
 Retrieve a child element by name and index.
 
bool isValid () const noexcept
 Check if the XML element is valid.
 
xmlNodePtr getNode () const noexcept
 Get the underlying XML node pointer.
 

Static Public Member Functions

static std::string getSafeAttribute (const XmlElement &element, const std::string_view name)
 Get the value of an attribute safely.
 

Detailed Description

Class representing a node in an XML document.

This class encapsulates an XML element, allowing users to access and manipulate element names, attributes, content, and children. It uses libxml2 for all operations and provides simplified methods to interact with XML nodes.

Definition at line 53 of file libxml_wrapper.h.

Constructor & Destructor Documentation

◆ XmlElement() [1/3]

XmlElement::XmlElement ( const xmlNode *  node)
explicit

Constructor for XmlElement.

Parameters
nodeThe xmlNode pointer representing the XML element.

Definition at line 63 of file libxml_wrapper.h.

63: _node(const_cast<xmlNode*>(node)) {}

◆ XmlElement() [2/3]

XmlElement::XmlElement ( const XmlElement )
default

◆ XmlElement() [3/3]

XmlElement::XmlElement ( XmlElement &&  )
defaultnoexcept

◆ ~XmlElement()

XmlElement::~XmlElement ( )
default

Member Function Documentation

◆ addChild()

XmlElement XmlElement::addChild ( const std::string_view  name) const
noexcept

Add a child element to the current node.

Parameters
nameThe name of the new child element.
Returns
The newly created XmlElement.

Definition at line 150 of file libxml_wrapper.h.

151 {
152 const xmlNode* child = xmlNewNode(nullptr, reinterpret_cast<const xmlChar*>(name.data()));
153 xmlAddChild(_node, const_cast<xmlNode*>(child));
154 return XmlElement(child);
155 }
Class representing a node in an XML document.
std::string_view name() const noexcept
Get the name of the XML element.

References name().

Referenced by serial::world_to_xml_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ childElement()

XmlElement XmlElement::childElement ( const std::string_view  name = "",
const unsigned  index = 0 
) const
noexcept

Retrieve a child element by name and index.

Parameters
nameThe name of the child element (optional).
indexThe index of the child to retrieve.
Returns
The child element or an invalid XmlElement if not found.

Definition at line 164 of file libxml_wrapper.h.

165 {
166 if (!_node)
167 {
168 return XmlElement(nullptr);
169 }
170 unsigned count = 0;
171 for (auto* child = _node->children; child; child = child->next)
172 {
173 if (child->type == XML_ELEMENT_NODE && (name.empty() || name == reinterpret_cast<const char*>(child->name)))
174 {
175 if (count == index)
176 {
177 return XmlElement(child);
178 }
179 ++count;
180 }
181 }
182 return XmlElement(nullptr);
183 }

References name().

Referenced by radar::FileTarget::FileTarget(), parseElements(), and removeIncludeElements().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getNode()

xmlNodePtr XmlElement::getNode ( ) const
noexcept

Get the underlying XML node pointer.

Returns
The underlying XML node pointer.

Definition at line 197 of file libxml_wrapper.h.

197{ return _node; }

Referenced by getSafeAttribute(), mergeXmlDocuments(), and XmlDocument::setRootElement().

+ Here is the caller graph for this function:

◆ getSafeAttribute()

static std::string XmlElement::getSafeAttribute ( const XmlElement element,
const std::string_view  name 
)
static

Get the value of an attribute safely.

Parameters
elementThe XmlElement to retrieve the attribute from.
nameThe name of the attribute.
Returns
The value of the attribute.
Exceptions
XmlExceptionif the attribute is not found.

Definition at line 117 of file libxml_wrapper.h.

118 {
119 std::string value;
120 if (xmlChar* attr = xmlGetProp(element.getNode(), reinterpret_cast<const xmlChar*>(name.data())))
121 {
122 value = reinterpret_cast<const char*>(attr);
123 xmlFree(attr);
124 }
125 else
126 {
127 throw XmlException("Attribute not found: " + std::string(name));
128 }
129 return value;
130 }
xmlNodePtr getNode() const noexcept
Get the underlying XML node pointer.
Exception class for handling XML-related errors.

References getNode(), and name().

+ Here is the call graph for this function:

◆ getText()

std::string XmlElement::getText ( ) const

Get the text content of the XML element.

Returns
The text content as a string.

Definition at line 87 of file libxml_wrapper.h.

88 {
89 if (!_node)
90 {
91 return "";
92 }
93 xmlChar* text = xmlNodeGetContent(_node);
94 std::string result = reinterpret_cast<const char*>(text);
95 xmlFree(text);
96 return result;
97 }

◆ isValid()

bool XmlElement::isValid ( ) const
noexcept

Check if the XML element is valid.

Returns
True if the node is valid, otherwise false.

Definition at line 190 of file libxml_wrapper.h.

190{ return _node != nullptr; }

Referenced by parseElements().

+ Here is the caller graph for this function:

◆ name()

std::string_view XmlElement::name ( ) const
noexcept

Get the name of the XML element.

Returns
The name of the XML element.

Definition at line 80 of file libxml_wrapper.h.

80{ return reinterpret_cast<const char*>(_node->name); }

Referenced by addChild(), childElement(), getSafeAttribute(), and setAttribute().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

XmlElement & XmlElement::operator= ( const XmlElement )
default

◆ operator=() [2/2]

XmlElement & XmlElement::operator= ( XmlElement &&  )
defaultnoexcept

◆ setAttribute()

void XmlElement::setAttribute ( const std::string_view  name,
const std::string_view  value 
) const

Set an attribute on the XML element.

Parameters
nameThe name of the attribute.
valueThe value to set for the attribute.

Definition at line 138 of file libxml_wrapper.h.

139 {
140 xmlSetProp(_node, reinterpret_cast<const xmlChar*>(name.data()),
141 reinterpret_cast<const xmlChar*>(value.data()));
142 }

References name().

Referenced by serial::world_to_xml_string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setText()

void XmlElement::setText ( const std::string_view  text) const

Set the text content of the XML element.

Parameters
textThe text to set as the content of the node.

Definition at line 104 of file libxml_wrapper.h.

105 {
106 xmlNodeSetContent(_node, reinterpret_cast<const xmlChar*>(text.data()));
107 }

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