19#include <libxml/parser.h>
28#include "libxml/globals.h"
29#include "libxml/xmlstring.h"
43 explicit XmlException(
const std::string_view message) : std::runtime_error(std::string(message)) {}
64 explicit XmlElement(
const xmlNode* node) : _node(const_cast<xmlNode*>(node)) {}
81 [[nodiscard]] std::string_view
name() const noexcept {
return reinterpret_cast<const char*
>(_node->name); }
94 xmlChar* text = xmlNodeGetContent(_node);
95 std::string result =
reinterpret_cast<const char*
>(text);
105 void setText(
const std::string_view text)
const
107 xmlNodeSetContent(_node,
reinterpret_cast<const xmlChar*
>(text.data()));
121 if (xmlChar* attr = xmlGetProp(element.
getNode(),
reinterpret_cast<const xmlChar*
>(
name.data())))
123 value =
reinterpret_cast<const char*
>(attr);
146 if (xmlChar* attr = xmlGetProp(element.
getNode(),
reinterpret_cast<const xmlChar*
>(
name.data())))
148 std::string value =
reinterpret_cast<const char*
>(attr);
163 xmlSetProp(_node,
reinterpret_cast<const xmlChar*
>(
name.data()),
164 reinterpret_cast<const xmlChar*
>(value.data()));
175 const xmlNode* child = xmlNewNode(
nullptr,
reinterpret_cast<const xmlChar*
>(
name.data()));
176 xmlAddChild(_node,
const_cast<xmlNode*
>(child));
189 if (_node ==
nullptr)
194 for (
auto* child = _node->children; child !=
nullptr; child = child->next)
196 if (child->type == XML_ELEMENT_NODE && (
name.empty() ||
name ==
reinterpret_cast<const char*
>(child->name)))
213 [[nodiscard]]
bool isValid() const noexcept {
return _node !=
nullptr; }
220 [[nodiscard]] xmlNodePtr
getNode() const noexcept {
return _node; }
229 std::unique_ptr<xmlDoc,
decltype(&xmlFreeDoc)> _doc;
237 XmlDocument() : _doc(xmlNewDoc(reinterpret_cast<const xmlChar*>(
"1.0")), &xmlFreeDoc)
261 [[nodiscard]]
bool loadFile(std::string_view filename);
269 [[nodiscard]]
bool loadString(const std::
string& content);
277 [[nodiscard]]
bool saveFile(const std::string_view filename)
const
284 return xmlSaveFormatFileEnc(filename.data(), _doc.get(),
"UTF-8", 1) != -1;
304 throw std::runtime_error(
"Document not created");
306 xmlDocSetRootElement(_doc.get(), root.
getNode());
319 throw std::runtime_error(
"Document not loaded");
321 const xmlNode* root = xmlDocGetRootElement(_doc.get());
324 throw std::runtime_error(
"Root element not found");
336 [[nodiscard]]
bool validateWithDtd(std::span<const unsigned char> dtdData)
const;
345 [[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::optional< std::string > getOptionalAttribute(const XmlElement &element, const std::string_view name)
Get the value of an optional attribute.
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.