libcommonc++  0.7
XMLElement Class Reference

An object representing an XML element. More...

#include <XMLElement.h++>

Collaboration diagram for XMLElement:

Public Member Functions

String getName () const
 Get the name of the element. More...
 
void setAttribute (const String &name, const String &value)
 Set a String attribute in the element. More...
 
void setAttribute (const String &name, int value)
 Set an integer attribute in the element. More...
 
void setAttribute (const String &name, const int64_t &value)
 Set a 64-bit integer attribute in the element. More...
 
void setAttribute (const String &name, const double &value)
 Set a floating point attribute in the element. More...
 
void setAttribute (const String &name, bool value)
 Set a boolean attribute in the element. More...
 
XMLElementgetParent ()
 Get the parent element of this element. More...
 
const XMLElementgetParent () const
 Get the parent element of this element. More...
 
bool getAttribute (const String &name, String &value, const String &defaultValue="") const
 Get the value of a String attribute in the element. More...
 
bool getAttribute (const String &name, int &value, int defaultValue=0) const
 Get the value of an integer attribute in the element. More...
 
bool getAttribute (const String &name, int64_t &value, const int64_t &defaultValue=0) const
 Get the value of a 64-bit integer attribute in the element. More...
 
bool getAttribute (const String &name, bool &value, bool defaultValue=false) const
 Get the value of a boolean attribute in the element. More...
 
bool getAttribute (const String &name, double &value, const double &defaultValue=0.0) const
 Get the value of a floating point attribute in the element. More...
 
void getAttributeNames (StringVec &names) const
 Get the names of all of the element's attributes. More...
 
void removeAttributes ()
 Remove all attributes from the element. More...
 
bool hasAttributes () const
 Test if the element has any attributes. More...
 
bool hasContent () const
 Test if the element has content. More...
 
void setContent (const String &content)
 Set the element's content. More...
 
const String getContent () const
 Get the element's content. More...
 
bool hasChildren () const
 Test if the element has children. More...
 
uint_t getChildCount () const
 Get the element's child count. More...
 
XMLElementaddChild (const String &name)
 Add a new child element to the element. More...
 
XMLElementgetChild (const String &name=String::null)
 Get the first child of the element. More...
 
const XMLElementgetChild (const String &name=String::null) const
 Get the first child of the element. More...
 
XMLElementgetChild (uint_t index)
 Get the child at the given index. More...
 
const XMLElementgetChild (uint_t index) const
 Get the child at the given index. More...
 
bool hasChild (const String &name) const
 Test if the element has at least one child element with the given name. More...
 
XMLElementoperator[] (const String &name)
 Get the first child with the given name. More...
 
XMLElementoperator[] (const char *name)
 Get the first child with the given name. More...
 
const XMLElementoperator[] (const String &name) const
 Get the first child with the given name. More...
 
const XMLElementoperator[] (const char *name) const
 Get the first child with the given name. More...
 
XMLElementoperator[] (int index)
 Get the child at the given index. More...
 
const XMLElementoperator[] (int index) const
 Get the child at the given index. More...
 
void removeChildren ()
 Remove (and delete) all of the child elements. More...
 
void removeChildren (const String &name)
 Remove (and delete) all of the child elements with the given name. More...
 
XMLElementoperator= (bool val)
 Set the element's content to the string representation of a bool. More...
 
XMLElementoperator= (long val)
 Set the element's content to the string representation of a long. More...
 
XMLElementoperator= (unsigned long val)
 Set the element's content to the string representation of an unsigned long. More...
 
XMLElementoperator= (const int64_t &val)
 Set the element's content to the string representation of an int64_t. More...
 
XMLElementoperator= (const uint64_t &val)
 Set the element's content to the string representation of a uint64_t. More...
 
XMLElementoperator= (float val)
 Set the element's content to the string representation of a float. More...
 
XMLElementoperator= (const double &val)
 Set the element's content to the string representation of a double. More...
 
XMLElementoperator= (const String &val)
 Set the element's content. More...
 
bool toBool () const
 Return the element's content as a bool. More...
 
int toInt () const
 Return the element's content as a long. More...
 
uint_t toUInt () const
 Return the element's content as an unsigned long. More...
 
int64_t toInt64 () const
 Return the element's content as an int64_t. More...
 
uint64_t toUInt64 () const
 Return the element's content as a uint64_t. More...
 
float toFloat () const
 Return the element's content as a float. More...
 
double toDouble () const
 Return the element's content as a double. More...
 
String toString () const
 Return the element's content as a String. More...
 
bool isNull () const
 Test if the element is a "null" element. More...
 
 operator const void * () const
 Boolean evaluation cast operator. More...
 
bool operator! () const
 Test if the element is a "null" element. More...
 
void write (std::ostream &stream, int depth, uint_t tabWidth) const
 Write the element (and its children, recursively) to a stream. More...
 

Static Public Member Functions

static void validateName (const String &name)
 Determine if an element or attribute name is valid. More...
 

Static Public Attributes

static XMLElement null
 The "null" element. More...
 

Friends

class XMLDocument
 
class XMLElementConstIterator
 
class XMLElementIterator
 

Detailed Description

An object representing an XML element.

XMLElement objects cannot be constructed directly by the caller, to reinforce the convention that they are owned by their enclosing XMLDocument.

All methods in this class return XMLElement::null if a matching or requested element was not found; this is a special instance of XMLElement for which isNull() returns true and whose mutator methods are all no-ops.

To iterate over the child elements of a given element elem, use an XMLElementIterator or XMLElementConstIterator.

Author
Mark Lindner

Member Function Documentation

◆ addChild()

XMLElement & addChild ( const String name)

Add a new child element to the element.

Parameters
nameThe name for the new element.
Returns
A reference to the new XMLElement.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ getAttribute() [1/5]

bool getAttribute ( const String name,
String value,
const String defaultValue = "" 
) const

Get the value of a String attribute in the element.

Parameters
nameThe attribute name.
valueThe value to fill in.
defaultValueThe value to fill in if the element has no attribute with the given name.
Returns
true if the value was found, false if the default value was filled in.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ getAttribute() [2/5]

bool getAttribute ( const String name,
int &  value,
int  defaultValue = 0 
) const

Get the value of an integer attribute in the element.

Parameters
nameThe attribute name.
valueThe value to fill in.
defaultValueThe value to fill in if the element has no attribute with the given name.
Returns
true if the value was found, false if the default value was filled in.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ getAttribute() [3/5]

bool getAttribute ( const String name,
int64_t &  value,
const int64_t &  defaultValue = 0 
) const

Get the value of a 64-bit integer attribute in the element.

Parameters
nameThe attribute name.
valueThe value to fill in.
defaultValueThe value to fill in if the element has no attribute with the given name.
Returns
true if the value was found, false if the default value was filled in.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ getAttribute() [4/5]

bool getAttribute ( const String name,
bool &  value,
bool  defaultValue = false 
) const

Get the value of a boolean attribute in the element.

Parameters
nameThe attribute name.
valueThe value to fill in.
defaultValueThe value to fill in if the element has no attribute with the given name.
Returns
true if the value was found, false if the default value was filled in.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ getAttribute() [5/5]

bool getAttribute ( const String name,
double &  value,
const double &  defaultValue = 0.0 
) const

Get the value of a floating point attribute in the element.

Parameters
nameThe attribute name.
valueThe value to fill in.
defaultValueThe value to fill in if the element has no attribute with the given name.
Returns
true if the value was found, false if the default value was filled in.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ getAttributeNames()

void getAttributeNames ( StringVec names) const

Get the names of all of the element's attributes.

Parameters
namesThe vector in which to place the names.

◆ getChild() [1/4]

XMLElement & getChild ( const String name = String::null)

Get the first child of the element.

Parameters
nameIf not String::null, gets the firt child element with the given name.
Returns
The first child element, or XMLElement::null if no matching element was found.

◆ getChild() [2/4]

const XMLElement & getChild ( const String name = String::null) const

Get the first child of the element.

Parameters
nameIf not String::null, gets the firt child element with the given name.
Returns
The first child element, or XMLElement::null if no matching element was found.

◆ getChild() [3/4]

XMLElement & getChild ( uint_t  index)

Get the child at the given index.

Parameters
indexThe index.
Returns
The child element, or XMLElement::null if the index is out of range.

◆ getChild() [4/4]

const XMLElement & getChild ( uint_t  index) const

Get the child at the given index.

Parameters
indexThe index.
Returns
The child element, or XMLElement::null if the index is out of range.

◆ getChildCount()

uint_t getChildCount ( ) const

Get the element's child count.

Returns
The number of child elements.

◆ getContent()

const String getContent ( ) const
inline

Get the element's content.

Returns
The content.

◆ getName()

String getName ( ) const
inline

Get the name of the element.

◆ getParent() [1/2]

XMLElement & getParent ( )

Get the parent element of this element.

Returns
The parent element, or XMLElement::null if the element has no parent (i.e., if it is the root element).

◆ getParent() [2/2]

const XMLElement & getParent ( ) const

Get the parent element of this element.

Returns
The parent element, or XMLElement::null if the element has no parent (i.e., if it is the root element).

◆ hasAttributes()

bool hasAttributes ( ) const

Test if the element has any attributes.

Returns
true if the element has at least one attribute, false otherwise.

◆ hasChild()

bool hasChild ( const String name) const

Test if the element has at least one child element with the given name.

Parameters
nameThe name.
Returns
true if a child with the given name exists, false otherwise.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ hasChildren()

bool hasChildren ( ) const

Test if the element has children.

Returns
true if the element has at least one child, false otherwise.

◆ hasContent()

bool hasContent ( ) const
inline

Test if the element has content.

Returns
true if the element has content (including content that consists solely of whitespace characters), false otherwise.

◆ isNull()

bool isNull ( ) const
inline

Test if the element is a "null" element.

◆ operator const void *()

operator const void * ( ) const
inline

Boolean evaluation cast operator.

◆ operator!()

bool operator! ( ) const
inline

Test if the element is a "null" element.

◆ operator=() [1/8]

XMLElement & operator= ( bool  val)

Set the element's content to the string representation of a bool.

◆ operator=() [2/8]

XMLElement & operator= ( long  val)

Set the element's content to the string representation of a long.

◆ operator=() [3/8]

XMLElement & operator= ( unsigned long  val)

Set the element's content to the string representation of an unsigned long.

◆ operator=() [4/8]

XMLElement & operator= ( const int64_t &  val)

Set the element's content to the string representation of an int64_t.

◆ operator=() [5/8]

XMLElement & operator= ( const uint64_t &  val)

Set the element's content to the string representation of a uint64_t.

◆ operator=() [6/8]

XMLElement & operator= ( float  val)

Set the element's content to the string representation of a float.

◆ operator=() [7/8]

XMLElement & operator= ( const double &  val)

Set the element's content to the string representation of a double.

◆ operator=() [8/8]

XMLElement & operator= ( const String val)

Set the element's content.

Equivalent to setContent().

◆ operator[]() [1/6]

XMLElement & operator[] ( const String name)

Get the first child with the given name.

If no such child exists, a new child with the given name is automatically added.

Parameters
nameThe name.
Returns
A reference to the child element.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ operator[]() [2/6]

XMLElement & operator[] ( const char *  name)

Get the first child with the given name.

If no such child exists, a new child with the given name is automatically added.

Parameters
nameThe name.
Returns
A reference to the child element.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ operator[]() [3/6]

const XMLElement& operator[] ( const String name) const
inline

Get the first child with the given name.

Parameters
nameThe name.
Returns
The first matching child element, or XMLElement::null if no matching element was found.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ operator[]() [4/6]

const XMLElement& operator[] ( const char *  name) const
inline

Get the first child with the given name.

Parameters
nameThe name.
Returns
The first matching child element, or XMLElement::null if no matching element was found.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ operator[]() [5/6]

XMLElement & operator[] ( int  index)

Get the child at the given index.

Parameters
indexThe index.
Returns
The child element, or XMLElement::null if if the index is out of range. throw OutOfBoundsException If the index is negative.

◆ operator[]() [6/6]

const XMLElement & operator[] ( int  index) const

Get the child at the given index.

Parameters
indexThe index.
Returns
A reference to the child element, or XMLElement::null if the index is out of range. throw OutOfBoundsException If the index is negative.

◆ removeAttributes()

void removeAttributes ( )

Remove all attributes from the element.

◆ removeChildren() [1/2]

void removeChildren ( )

Remove (and delete) all of the child elements.

◆ removeChildren() [2/2]

void removeChildren ( const String name)

Remove (and delete) all of the child elements with the given name.

Parameters
nameThe name.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ setAttribute() [1/5]

void setAttribute ( const String name,
const String value 
)

Set a String attribute in the element.

Parameters
nameThe attribute name.
valueThe attribute value. If the element already contains an attribute with the given name, the old value is overwritten.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ setAttribute() [2/5]

void setAttribute ( const String name,
int  value 
)

Set an integer attribute in the element.

Parameters
nameThe attribute name.
valueThe attribute value. If the element already contains an attribute with the given name, the old value is overwritten.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ setAttribute() [3/5]

void setAttribute ( const String name,
const int64_t &  value 
)

Set a 64-bit integer attribute in the element.

Parameters
nameThe attribute name.
valueThe attribute value. If the element already contains an attribute with the given name, the old value is overwritten.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ setAttribute() [4/5]

void setAttribute ( const String name,
const double &  value 
)

Set a floating point attribute in the element.

Parameters
nameThe attribute name.
valueThe attribute value. If the element already contains an attribute with the given name, the old value is overwritten.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ setAttribute() [5/5]

void setAttribute ( const String name,
bool  value 
)

Set a boolean attribute in the element.

Parameters
nameThe attribute name.
valueThe attribute value. If the element already contains an attribute with the given name, the old value is overwritten.
Exceptions
InvalidArgumentExceptionIf the name is invalid.

◆ setContent()

void setContent ( const String content)
inline

Set the element's content.

Parameters
contentThe content.

◆ toBool()

bool toBool ( ) const

Return the element's content as a bool.

◆ toDouble()

double toDouble ( ) const

Return the element's content as a double.

◆ toFloat()

float toFloat ( ) const

Return the element's content as a float.

◆ toInt()

int toInt ( ) const

Return the element's content as a long.

◆ toInt64()

int64_t toInt64 ( ) const

Return the element's content as an int64_t.

◆ toString()

String toString ( ) const
inline

Return the element's content as a String.

Equivalent to getContent().

◆ toUInt()

uint_t toUInt ( ) const

Return the element's content as an unsigned long.

◆ toUInt64()

uint64_t toUInt64 ( ) const

Return the element's content as a uint64_t.

◆ validateName()

void validateName ( const String name)
static

Determine if an element or attribute name is valid.

◆ write()

void write ( std::ostream &  stream,
int  depth,
uint_t  tabWidth 
) const

Write the element (and its children, recursively) to a stream.

Friends And Related Function Documentation

◆ XMLDocument

friend class XMLDocument
friend

◆ XMLElementConstIterator

friend class XMLElementConstIterator
friend

◆ XMLElementIterator

friend class XMLElementIterator
friend

Member Data Documentation

◆ null

XMLElement null
static

The "null" element.


The documentation for this class was generated from the following files: