31 static void xmlerror(
int err,
const char *str1,
const char *str2)
123 return node->
ToText() !=
nullptr;
268 void Element::count_subnondes()
533 newel.SetAttribute(a.GetName(), a.GetValue<
StringUTF8>());
536 for (
Node sn = el.BeginNode(); sn != el.EndNode(); ++sn)
537 newel.PushBackClone(sn, recursive);
643 const char *val = element->
Attribute(
conv->FromUTF8(name).c_str());
646 value =
conv->ToUTF8(val);
658 void Element::queryAttribute(
const StringUTF8 &name,
int &value)
const
675 void Element::queryAttribute(
const StringUTF8 &name,
unsigned int &value)
const
692 void Element::queryAttribute(
const StringUTF8 &name,
bool &value)
const
709 void Element::queryAttribute(
const StringUTF8 &name,
double &value)
const
726 void Element::queryAttribute(
const StringUTF8 &name,
float &value)
const
771 return Attribute(const_cast<tinyxml2::XMLAttribute*>(attr->Next()),
conv);
779 return conv->ToUTF8(attr->Name());
789 attr->SetAttribute(
conv->FromUTF8(value).c_str());
795 void Element::Attribute::queryValue(
StringUTF8 &value)
const
797 const char *val = attr->Value();
798 value =
conv->ToUTF8(val);
805 void Element::Attribute::queryValue(
int &value)
const
807 int res = attr->QueryIntValue(&value);
808 xmlerror(res,
nullptr,
nullptr);
815 void Element::Attribute::queryValue(
unsigned int &value)
const
817 int res = attr->QueryUnsignedValue(&value);
818 xmlerror(res,
nullptr,
nullptr);
825 void Element::Attribute::queryValue(
bool &value)
const
827 int res = attr->QueryBoolValue(&value);
828 xmlerror(res,
nullptr,
nullptr);
835 void Element::Attribute::queryValue(
double &value)
const
837 int res = attr->QueryDoubleValue(&value);
838 xmlerror(res,
nullptr,
nullptr);
845 void Element::Attribute::queryValue(
float &value)
const
847 int res = attr->QueryFloatValue(&value);
848 xmlerror(res,
nullptr,
nullptr);
882 return text->
CData();
891 return text->
Value();
908 doc(std::make_unique<tinyxml2::XMLDocument>()),
913 StringUTF8 decl =
"xml version=\"" + version +
"\" encoding=\"" + encoding +
"\"";
914 doc->InsertEndChild(doc->NewDeclaration(
conv->FromUTF8(decl).c_str()));
925 doc(std::make_unique<tinyxml2::XMLDocument>()),
928 int res = doc->LoadFile(fname.
CStr());
932 if (doc->GetErrorStr1())
933 s1 = doc->GetErrorStr1();
934 if (doc->GetErrorStr2())
935 s2 = doc->GetErrorStr2();
947 size_t beg = val.
Find(ekey);
955 size_t en = val.
Find(sep, beg);
962 beg = val.
Find(vkey);
970 size_t en = val.
Find(sep, beg);
979 conv = std::make_shared<CharsetConverter>(enc.Std(),
true, char_conversion_throws);
988 doc(std::make_unique<tinyxml2::XMLDocument>()),
993 int res = doc->Parse(content);
994 xmlerror(res, doc->GetErrorStr1(), doc->GetErrorStr2());
1011 int res = doc->SaveFile(fname.
CStr());
1012 xmlerror(res, doc->GetErrorStr1(), doc->GetErrorStr2());
1026 int res = doc->SaveFile(filename.
CStr());
1027 xmlerror(res, doc->GetErrorStr1(), doc->GetErrorStr2());
1047 return Node(doc->FirstChild(), conv);
1055 return Node(doc->LastChild(), conv);
1066 return Element(doc->FirstChildElement(name.
IsEmpty() ?
nullptr : conv->FromUTF8(name).c_str()), conv);
1077 return Element(doc->LastChildElement(name.
IsEmpty() ?
nullptr : conv->FromUTF8(name).c_str()), conv);
1092 doc->InsertEndChild(el);
1112 if (!doc->InsertAfterChild(n.node, el))
1114 doc->GetDocument()->DeleteNode(el);
1129 doc->InsertEndChild(c);
1146 if (!doc->InsertAfterChild(n.node, c))
1148 doc->GetDocument()->DeleteNode(c);
1170 newel.SetAttribute(a.GetName(), a.GetValue<
StringUTF8>());
1173 for (
Node sn = el.BeginNode(); sn != el.EndNode(); ++sn)
1174 newel.PushBackClone(sn, recursive);
1190 tinyxml2::XMLText *tx = doc->NewText(t.IsCData() ? t.GetValue().CStr() : conv->FromUTF8(t.GetValue()).c_str());
1191 tx->SetCData(t.IsCData());
1192 doc->InsertEndChild(tx);
1193 return Text(tx, conv);
1201 doc->InsertEndChild(nde);
1202 return Node(nde, conv);
1209 doc->InsertEndChild(nun);
1210 return Node(nun, conv);
void SetValue(const char *val, bool staticMem=false)
Attribute & operator++()
Move to next attribute.
void Save()
Saves to file.
bool CData() const
Returns true if this is a CDATA text element.
const XMLNode * Parent() const
Get the parent of this node on the DOM.
bool IsText()
Checks if the node is a text.
virtual StringUTF8 GetValue() const override
Gets the content of the node.
Comment PushFrontComment(const StringUTF8 &text)
Adds a comment at the front of the children list.
Node PushBackClone(Node &n, bool recursive=false)
Adds a copy of a node at the end of the children list.
XMLNode * InsertEndChild(XMLNode *addThis)
void Clear()
Removes all children.
Text(const Text &)=default
XMLError QueryBoolAttribute(const char *name, bool *value) const
See QueryIntAttribute()
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
virtual XMLText * ToText()
Safely cast to Text, or null.
Comment PushBackComment(const StringUTF8 &text)
Adds a comment at the end of the children list.
const char * Value() const
XMLText * NewText(const char *text)
XMLComment * NewComment(const char *comment)
const XMLNode * LastChild() const
Get the last child node, or null if none exists.
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
bool IsComment()
Checks if the node is a comment.
Element EndElement()
Gets a null node.
Element(const Element &)=default
Text AsText()
Converts to text.
Unintialized object error.
Element GetRoot()
Gets the first element.
XMLError QueryUnsignedAttribute(const char *name, unsigned int *value) const
See QueryIntAttribute()
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
Element AsElement()
Converts to element.
Attribute EndAttribute()
Gets the null attribute.
Element InsertElement(Node &n, const StringUTF8 &name)
Inserts an element after a node.
Element GetLastElement(const StringUTF8 &name="")
Gets the last child element.
Element GetNextSiblingElement(const StringUTF8 &name="")
Gets the next sibling element.
void DeleteNode(XMLNode *node)
const char * Attribute(const char *name, const char *value=0) const
void DeleteChild(XMLNode *node)
XMLError QueryFloatAttribute(const char *name, float *value) const
See QueryIntAttribute()
Text PushBackText(const StringUTF8 &text, bool cdata=false)
Adds a text at the end of the children list.
Element GetLastChildElement(const StringUTF8 &name="")
Gets the last child element.
Text PushFrontText(const StringUTF8 &text, bool cdata=false)
Adds a text at the front of the children list.
Node GetLastNode()
Gets the last child node.
const XMLElement * NextSiblingElement(const char *name=0) const
Get the next (right) sibling element of this node, with an optionally supplied name.
Node GetParent()
Gets the parent node if any.
Comment InsertComment(Node &n, const StringUTF8 &text)
Inserts a comment after a node.
StringUTF8 SubString(size_t pos, size_t n=0) const
Extracts a part of the string.
Document & operator=(const Document &)=delete
const char * CStr() const noexcept
Conversion to UTF8 cstring.
Element GetFirstChildElement(const StringUTF8 &name="")
Gets the first child element.
Node GetLastChild()
Gets the last child node.
bool IsEmpty() const noexcept
Checks if the string is empty.
A convenience class for file paths.
const XMLElement * PreviousSiblingElement(const char *name=0) const
Get the previous (left) sibling element of this node, with an optionally supplied name...
bool IsElement()
Checks if the node is an element.
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
virtual StringUTF8 GetValue() const
Gets the content of the node.
Element GetPreviousSiblingElement(const StringUTF8 &name="")
Gets the previous sibling element.
Node GetFirstNode()
Gets the first child node.
void SetValue(const StringUTF8 &s)
Sets the content of the node.
Element BeginElement()
Gets the first child element.
Document(const StringUTF8 &encoding="UTF-8", const StringUTF8 &version="1.0", bool char_conversion_throws=true)
Constructor.
void SetAttribute(const StringUTF8 &name, const StringUTF8 &value)
Sets the value of an attribute.
static size_t NPos() noexcept
Last position in a string.
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
StringUTF8 AsString()
Exports the document to a string.
Element & operator++()
Moves to the next sibling element.
XMLElement * NewElement(const char *name)
void SetValue(const StringUTF8 &value)
Sets the value of the attribute.
StringUTF8 GetName() const
Gets the name of the attribute.
const char * GetErrorStr2() const
Return a possibly helpful secondary diagnostic location or string.
Node GetFirstChild()
Gets the first child node.
Element GetFirstElement(const StringUTF8 &name="")
Gets the first child element.
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
Comment PushBackComment(const StringUTF8 &text)
Adds a comment at the end of the children list.
const char * CStr() const
XMLError QueryIntAttribute(const char *name, int *value) const
Attribute Next()
Gets next attribute.
Text InsertText(Node &n, const StringUTF8 &text, bool cdata=false)
Inserts a text after a node.
const XMLAttribute * Next() const
The next attribute in the list.
const char * GetErrorStr1() const
Return a possibly helpful diagnostic location or string.
const XMLElement * LastChildElement(const char *name=0) const
Node & operator++()
Moves to the next sibling node.
Node GetPreviousSibling()
Gets the previous sibling node.
XMLUnknown * NewUnknown(const char *text)
Node GetNextSibling()
Gets the next sibling node.
Attribute BeginAttribute()
Gets the first attribute.
const XMLNode * PreviousSibling() const
Get the previous (left) sibling node of this node.
StringUTF8 GetFirstChildText()
Gets the first child as text.
const XMLElement * FirstChildElement(const char *name=0) const
Element InsertElement(Node &n, const StringUTF8 &name)
Inserts an element after a node.
size_t Size() const noexcept
Returns the number of bytes in the string.
XMLNode * InsertAfterChild(XMLNode *afterThis, XMLNode *addThis)
void DeleteAttribute(const char *name)
Element PushFrontElement(const StringUTF8 &name)
Adds an element at the front of the children list.
Comment AsComment()
Converts to comment.
A character string class.
size_t Find(const StringUTF8 &s, size_t from_pos=0) const
Finds the first occurrence of a string.
Node PushBackClone(Node &n, bool recursive=false)
Adds a copy of a node at the end of the children list.
Node(const Node &)=default
bool IsCData() const
Is the text a CData?
virtual XMLUnknown * ToUnknown()
Safely cast to an Unknown, or null.
XMLError QueryDoubleAttribute(const char *name, double *value) const
See QueryIntAttribute()
virtual ~Document()
Destructor.
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
virtual XMLDeclaration * ToDeclaration()
Safely cast to a Declaration, or null.
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
void RemoveChild(Node &n)
Removes a child node.
Comment InsertComment(Node &n, const StringUTF8 &text)
Inserts a comment after a node.
Invalid argument error (e.g.: nullptr pointer)
Attribute(const Attribute &)=default
An item was not found in a container.
void RemoveAttribute(const StringUTF8 &name)
Removes an attribute.
XMLNode * InsertFirstChild(XMLNode *addThis)
virtual XMLComment * ToComment()
Safely cast to a Comment, or null.