19#include <libxml/parser.h>
27#include "libxml/globals.h"
28#include "libxml/xmlstring.h"
42 explicit XmlException(
const std::string_view message) : std::runtime_error(std::string(message)) {}
63 explicit XmlElement(
const xmlNode* node) : _node(const_cast<xmlNode*>(node)) {}
80 [[nodiscard]] std::string_view
name() const noexcept {
return reinterpret_cast<const char*
>(_node->name); }
93 xmlChar* text = xmlNodeGetContent(_node);
94 std::string result =
reinterpret_cast<const char*
>(text);
104 void setText(
const std::string_view text)
const
106 xmlNodeSetContent(_node,
reinterpret_cast<const xmlChar*
>(text.data()));
120 if (xmlChar* attr = xmlGetProp(element.
getNode(),
reinterpret_cast<const xmlChar*
>(
name.data())))
122 value =
reinterpret_cast<const char*
>(attr);
140 xmlSetProp(_node,
reinterpret_cast<const xmlChar*
>(
name.data()),
141 reinterpret_cast<const xmlChar*
>(value.data()));
152 const xmlNode* child = xmlNewNode(
nullptr,
reinterpret_cast<const xmlChar*
>(
name.data()));
153 xmlAddChild(_node,
const_cast<xmlNode*
>(child));
171 for (
auto* child = _node->children; child; child = child->next)
173 if (child->type == XML_ELEMENT_NODE && (
name.empty() ||
name ==
reinterpret_cast<const char*
>(child->name)))
190 [[nodiscard]]
bool isValid() const noexcept {
return _node !=
nullptr; }
197 [[nodiscard]] xmlNodePtr
getNode() const noexcept {
return _node; }
206 std::unique_ptr<xmlDoc,
decltype(&xmlFreeDoc)> _doc;
214 XmlDocument() : _doc(xmlNewDoc(reinterpret_cast<const xmlChar*>(
"1.0")), &xmlFreeDoc)
238 [[nodiscard]]
bool loadFile(std::string_view filename);
246 [[nodiscard]]
bool loadString(const std::
string& content);
254 [[nodiscard]]
bool saveFile(const std::string_view filename)
const
261 return xmlSaveFormatFileEnc(filename.data(), _doc.get(),
"UTF-8", 1) != -1;
281 throw std::runtime_error(
"Document not created");
283 xmlDocSetRootElement(_doc.get(), root.
getNode());
296 throw std::runtime_error(
"Document not loaded");
298 const xmlNode* root = xmlDocGetRootElement(_doc.get());
301 throw std::runtime_error(
"Root element not found");
313 [[nodiscard]]
bool validateWithDtd(std::span<const unsigned char> dtdData)
const;
322 [[nodiscard]]
bool validateWithXsd(std::span<const unsigned char> xsdData)
const;
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 addChild(const std::string_view name) const noexcept
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.
std::string_view name() const noexcept
Get the name of the XML element.
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
XmlElement(const xmlNode *node)
Constructor for XmlElement.
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.
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.