|
| XmlElement (const String &tagName) |
|
| XmlElement (const char *tagName) |
|
| XmlElement (const Identifier &tagName) |
|
| XmlElement (StringRef tagName) |
|
| XmlElement (String::CharPointerType tagNameBegin, String::CharPointerType tagNameEnd) |
|
| XmlElement (const XmlElement &) |
|
XmlElement & | operator= (const XmlElement &) |
|
| ~XmlElement () noexcept |
|
bool | isEquivalentTo (const XmlElement *other, bool ignoreOrderOfAttributes) const noexcept |
|
String | createDocument (StringRef dtdToUse, bool allOnOneLine=false, bool includeXmlHeader=true, StringRef encodingType="UTF-8", int lineWrapLength=60) const |
|
void | writeToStream (OutputStream &output, StringRef dtdToUse, bool allOnOneLine=false, bool includeXmlHeader=true, StringRef encodingType="UTF-8", int lineWrapLength=60) const |
|
bool | writeToFile (const File &destinationFile, StringRef dtdToUse, StringRef encodingType="UTF-8", int lineWrapLength=60) const |
|
const String & | getTagName () const noexcept |
|
String | getNamespace () const |
|
String | getTagNameWithoutNamespace () const |
|
bool | hasTagName (StringRef possibleTagName) const noexcept |
|
bool | hasTagNameIgnoringNamespace (StringRef possibleTagName) const |
|
int | getNumAttributes () const noexcept |
|
const String & | getAttributeName (int attributeIndex) const noexcept |
|
const String & | getAttributeValue (int attributeIndex) const noexcept |
|
bool | hasAttribute (StringRef attributeName) const noexcept |
|
const String & | getStringAttribute (StringRef attributeName) const noexcept |
|
String | getStringAttribute (StringRef attributeName, const String &defaultReturnValue) const |
|
bool | compareAttribute (StringRef attributeName, StringRef stringToCompareAgainst, bool ignoreCase=false) const noexcept |
|
int | getIntAttribute (StringRef attributeName, int defaultReturnValue=0) const |
|
double | getDoubleAttribute (StringRef attributeName, double defaultReturnValue=0.0) const |
|
bool | getBoolAttribute (StringRef attributeName, bool defaultReturnValue=false) const |
|
void | setAttribute (const Identifier &attributeName, const String &newValue) |
|
void | setAttribute (const Identifier &attributeName, int newValue) |
|
void | setAttribute (const Identifier &attributeName, double newValue) |
|
void | removeAttribute (const Identifier &attributeName) noexcept |
|
void | removeAllAttributes () noexcept |
|
XmlElement * | getFirstChildElement () const noexcept |
|
XmlElement * | getNextElement () const noexcept |
|
XmlElement * | getNextElementWithTagName (StringRef requiredTagName) const |
|
int | getNumChildElements () const noexcept |
|
XmlElement * | getChildElement (int index) const noexcept |
|
XmlElement * | getChildByName (StringRef tagNameToLookFor) const noexcept |
|
XmlElement * | getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept |
|
void | addChildElement (XmlElement *newChildElement) noexcept |
|
void | insertChildElement (XmlElement *newChildElement, int indexToInsertAt) noexcept |
|
void | prependChildElement (XmlElement *newChildElement) noexcept |
|
XmlElement * | createNewChildElement (StringRef tagName) |
|
bool | replaceChildElement (XmlElement *currentChildElement, XmlElement *newChildNode) noexcept |
|
void | removeChildElement (XmlElement *childToRemove, bool shouldDeleteTheChild) noexcept |
|
void | deleteAllChildElements () noexcept |
|
void | deleteAllChildElementsWithTagName (StringRef tagName) noexcept |
|
bool | containsChildElement (const XmlElement *possibleChild) const noexcept |
|
XmlElement * | findParentElementOf (const XmlElement *childToSearchFor) noexcept |
|
template<class ElementComparator > |
void | sortChildElements (ElementComparator &comparator, bool retainOrderOfEquivalentItems=false) |
|
bool | isTextElement () const noexcept |
|
const String & | getText () const noexcept |
|
void | setText (const String &newText) |
|
String | getAllSubText () const |
|
String | getChildElementAllSubText (StringRef childTagName, const String &defaultReturnValue) const |
|
void | addTextElement (const String &text) |
|
void | deleteAllTextElements () noexcept |
|
Used to build a tree of elements representing an XML document.
An XML document can be parsed into a tree of XmlElements, each of which represents an XML tag structure, and which may itself contain other nested elements.
An XmlElement can also be converted back into a text document, and has lots of useful methods for manipulating its attributes and sub-elements, so XmlElements can actually be used as a handy general-purpose data structure.
Here's an example of parsing some elements:
if (myElement->hasTagName ("ANIMALS"))
{
{
if (e->hasTagName ("GIRAFFE"))
{
String giraffeName = e->getStringAttribute (
"name");
int giraffeAge = e->getIntAttribute ("age");
bool isFriendly = e->getBoolAttribute ("friendly");
}
}
}
And here's an example of how to create an XML document from scratch:
for (int i = 0; i < numAnimals; ++i)
{
animalsList.addChildElement (giraffe);
}
- See also
- XmlDocument
void XmlElement::addChildElement |
( |
XmlElement * |
newChildElement | ) |
|
|
noexcept |
Recursively searches all sub-elements of this one, looking for an element which is the direct parent of the specified element.
Because elements don't store a pointer to their parent, if you have one and need to find its parent, the only way to do so is to exhaustively search the whole tree for it.
If the given child is found somewhere in this element's hierarchy, then this method will return its parent. If not, it will return nullptr.
String XmlElement::getAllSubText |
( |
| ) |
const |
Returns all the text from this element's child nodes.
This iterates all the child elements and when it finds text elements, it concatenates their text into a big string which it returns.
E.g.
<xyz>hello <x>there</x> world</xyz>
if you called getAllSubText on the "xyz" element, it'd return "hello there world".
Note that leading and trailing whitespace will be included in the string - to remove if, just call String::trim() on the result.
- See also
- isTextElement, getChildElementAllSubText, getText, addTextElement
bool XmlElement::getBoolAttribute |
( |
StringRef |
attributeName, |
|
|
bool |
defaultReturnValue = false |
|
) |
| const |
Returns the value of a named attribute as a boolean.
This will try to find the attribute and interpret it as a boolean. To do this, it'll return true if the value is "1", "true", "y", etc, or false for other values.
- Parameters
-
attributeName | the name of the attribute to look up |
defaultReturnValue | a value to return if the element doesn't have an attribute with this name |
Returns the next of this element's siblings.
This can be used for iterating an element's sub-elements, e.g.
while (child != nullptr)
{
...do stuff with this child..
}
Note that when iterating the child elements, some of them might be text elements as well as XML tags - use isTextElement() to work this out.
Also, it's much easier and neater to use this method indirectly via the forEachXmlChildElement macro.
- Returns
- the sibling element that follows this one, or zero if this is the last element in its parent
- See also
- getNextElement, isTextElement, forEachXmlChildElement
Returns the text for a text element.
Note that if you have an element like this:
then calling getText on the "xyz" element won't return "hello", because that is actually stored in a special text sub-element inside the xyz element. To get the "hello" string, you could either call getText on the (unnamed) sub-element, or use getAllSubText() to do this automatically.
Note that leading and trailing whitespace will be included in the string - to remove if, just call String::trim() on the result.
- See also
- isTextElement, getAllSubText, getChildElementAllSubText
void XmlElement::prependChildElement |
( |
XmlElement * |
newChildElement | ) |
|
|
noexcept |
Inserts an element at the beginning of this element's list of children.
Child elements are deleted automatically when their parent is deleted, so make sure the object that you pass in will not be deleted by anything else, and make sure it's not already the child of another element.
Note that due to the XmlElement using a singly-linked-list, prependChildElement() is an O(1) operation, but addChildElement() is an O(N) operation - so if you're adding large number of elements, you may prefer to do so in reverse order!
- See also
- addChildElement, insertChildElement
Adds a named attribute to the element.
If the element already contains an attribute with this name, it's value will be updated to the new value. If there's no such attribute yet, a new one will be added.
Note that there are other setAttribute() methods that take integers, doubles, etc. to make it easy to store numbers.
- Parameters
-
attributeName | the name of the attribute to set |
newValue | the value to set it to |
- See also
- removeAttribute
void XmlElement::setAttribute |
( |
const Identifier & |
attributeName, |
|
|
int |
newValue |
|
) |
| |
Adds a named attribute to the element, setting it to an integer value.
If the element already contains an attribute with this name, it's value will be updated to the new value. If there's no such attribute yet, a new one will be added.
Note that there are other setAttribute() methods that take integers, doubles, etc. to make it easy to store numbers.
- Parameters
-
attributeName | the name of the attribute to set |
newValue | the value to set it to |
void XmlElement::setAttribute |
( |
const Identifier & |
attributeName, |
|
|
double |
newValue |
|
) |
| |
Adds a named attribute to the element, setting it to a floating-point value.
If the element already contains an attribute with this name, it's value will be updated to the new value. If there's no such attribute yet, a new one will be added.
Note that there are other setAttribute() methods that take integers, doubles, etc. to make it easy to store numbers.
- Parameters
-
attributeName | the name of the attribute to set |
newValue | the value to set it to |
template<class ElementComparator >
void XmlElement::sortChildElements |
( |
ElementComparator & |
comparator, |
|
|
bool |
retainOrderOfEquivalentItems = false |
|
) |
| |
|
inline |
Sorts the child elements using a comparator.
This will use a comparator object to sort the elements into order. The object passed must have a method of the form:
..and this method must return:
- a value of < 0 if the first comes before the second
- a value of 0 if the two objects are equivalent
- a value of > 0 if the second comes before the first
To improve performance, the compareElements() method can be declared as static or const.
- Parameters
-
comparator | the comparator to use for comparing elements. |
retainOrderOfEquivalentItems | if this is true, then items which the comparator says are equivalent will be kept in the order in which they currently appear in the array. This is slower to perform, but may be important in some cases. If it's false, a faster algorithm is used, but equivalent elements may be rearranged. |