14#include "libxml/encoding.h"
15#include "libxml/parser.h"
16#include "libxml/valid.h"
17#include "libxml/xmlIO.h"
18#include "libxml/xmlschemas.h"
23 xmlIOParseDTD(
nullptr,
24 xmlParserInputBufferCreateMem(
reinterpret_cast<const char*
>(dtdData.data()),
25 static_cast<int>(dtdData.size()), XML_CHAR_ENCODING_UTF8),
26 XML_CHAR_ENCODING_UTF8);
32 const std::unique_ptr<xmlValidCtxt,
decltype(&xmlFreeValidCtxt)> validation_ctxt(xmlNewValidCtxt(),
37 throw XmlException(
"Failed to create validation context.");
40 const bool is_valid = xmlValidateDtd(validation_ctxt.get(), _doc.get(), dtd);
53 const std::unique_ptr<xmlSchemaParserCtxt,
decltype(&xmlSchemaFreeParserCtxt)> schema_parser_ctxt(
54 xmlSchemaNewMemParserCtxt(
reinterpret_cast<const char*
>(xsdData.data()),
static_cast<int>(xsdData.size())),
55 xmlSchemaFreeParserCtxt);
56 if (!schema_parser_ctxt)
58 throw XmlException(
"Failed to create schema parser context.");
61 const std::unique_ptr<xmlSchema,
decltype(&xmlSchemaFree)> schema(xmlSchemaParse(schema_parser_ctxt.get()),
65 throw XmlException(
"Failed to parse schema from memory.");
68 const std::unique_ptr<xmlSchemaValidCtxt,
decltype(&xmlSchemaFreeValidCtxt)> schema_valid_ctxt(
69 xmlSchemaNewValidCtxt(schema.get()), xmlSchemaFreeValidCtxt);
70 if (!schema_valid_ctxt)
72 throw XmlException(
"Failed to create schema validation context.");
75 if (
const bool is_valid = xmlSchemaValidateDoc(schema_valid_ctxt.get(), _doc.get()) == 0; !is_valid)
88 for (xmlNodePtr child = included_root.
getNode()->children; child; child = child->next)
90 if (child->type == XML_ELEMENT_NODE)
92 xmlNodePtr new_node = xmlCopyNode(child, 1);
93 xmlAddChild(main_root.
getNode(), new_node);
106 xmlUnlinkNode(include_element.getNode());
107 xmlFreeNode(include_element.getNode());
118 _doc.reset(xmlReadFile(filename.data(),
nullptr, 0));
119 return _doc !=
nullptr;
124 _doc.reset(xmlReadMemory(content.c_str(),
static_cast<int>(content.length()),
"in_memory.xml",
nullptr, 0));
125 return _doc !=
nullptr;
135 xmlChar* buffer =
nullptr;
137 xmlDocDumpFormatMemory(_doc.get(), &buffer, &size, 1);
143 const std::string result(
reinterpret_cast<const char*
>(buffer),
static_cast<size_t>(size));
Class for managing XML documents.
XmlElement getRootElement() const
Get the root element of the document.
bool loadFile(std::string_view filename)
Load an XML file into the document.
bool validateWithDtd(std::span< const unsigned char > dtdData) const
Validate the document using a DTD.
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.
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.
xmlNodePtr getNode() const noexcept
Get the underlying XML node pointer.
Exception class for handling XML-related errors.
void mergeXmlDocuments(const XmlDocument &mainDoc, const XmlDocument &includedDoc)
Merge two XML documents.
void removeIncludeElements(const XmlDocument &doc)
Remove "include" elements from the XML document.
Wrapper for managing XML documents and elements using libxml2.
@ ERROR
Error level for error events.