19#include <libxml/parser.h>
30#include "libxml/globals.h"
31#include "libxml/xmlstring.h"
52 std::vector<xmlChar> _value;
57 _value.reserve(value.size() + 1);
60 _value.push_back(
static_cast<xmlChar>(
static_cast<unsigned char>(
character)));
69 const auto length = _value.empty() ?
static_cast<std::size_t
>(0) : _value.size() - 1;
70 if (length >
static_cast<std::size_t
>(std::numeric_limits<int>::max()))
72 throw XmlException(
"XML string exceeds libxml2 length limit.");
74 return static_cast<int>(length);
95 if (value ==
nullptr || length <= 0)
101 result.reserve(
static_cast<std::size_t
>(length));
102 for (
int index = 0; index < length; ++index)
104 result.push_back(
static_cast<char>(value[index]));
116 for (std::size_t index = 0; index < value.size(); ++index)
132 throw XmlException(
"Failed to create XML node: " + std::string(name));
199 if (_node ==
nullptr)
302 if (_node ==
nullptr)
417 throw std::runtime_error(
"Document not created");
432 throw std::runtime_error(
"Document not loaded");
437 throw std::runtime_error(
"Root element not found");
Class for managing XML documents.
XmlElement getRootElement() const
Get the root element of the document.
XmlDocument(XmlDocument &&) noexcept=default
bool loadFile(std::string_view filename)
Load an XML file into the document.
XmlDocument(const XmlDocument &)=delete
XmlDocument()
Constructor for XmlDocument.
bool validateWithDtd(std::span< const unsigned char > dtdData) const
Validate the document using a DTD.
void setRootElement(const XmlElement &root) const
Set the root element of the document.
bool validateWithXsd(std::span< const unsigned char > xsdData) const
Validate the document using an XSD schema.
bool loadString(const std::string &content)
Load an XML document from a string in memory.
bool saveFile(const std::string_view filename) const
Save the document to a file.
std::string dumpToString() const
Dumps the document to a string.
Class representing a node in an XML document.
XmlElement childElement(const std::string_view name="", const unsigned index=0) const noexcept
Retrieve a child element by name and index.
XmlElement(xmlNodePtr node)
Constructor for XmlElement.
static std::optional< std::string > getOptionalAttribute(const XmlElement &element, const std::string_view name)
Get the value of an optional attribute.
XmlElement addChild(const std::string_view name) const
Add a child element to the current node.
static std::string getSafeAttribute(const XmlElement &element, const std::string_view name)
Get the value of an attribute safely.
bool isValid() const noexcept
Check if the XML element is valid.
xmlNodePtr getNode() const noexcept
Get the underlying XML node pointer.
void setAttribute(const std::string_view name, const std::string_view value) const
Set an attribute on the XML element.
XmlElement(XmlElement &&) noexcept=default
static XmlElement create(const std::string_view name)
Create a new XML element by name.
std::string name() const
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.
XmlElement(const XmlElement &)=default
Exception class for handling XML-related errors.
XmlException(const std::string_view message)
Constructor for XmlException.
const xmlChar * c_str() const noexcept
XmlCharBuffer(const std::string_view value)
int contentLength() const
void mergeXmlDocuments(const XmlDocument &mainDoc, const XmlDocument &includedDoc)
Merge two XML documents.
void removeIncludeElements(const XmlDocument &doc)
Remove "include" elements from the XML document.
Header file for the logging system.
@ ERROR
Error level for error events.
bool equals(const xmlChar *xml_value, const std::string_view value)
xmlNodePtr createNode(const std::string_view name)
xmlDocPtr createDocument()
std::string toString(const xmlChar *value)