gloox  1.0
Classes | Public Types | Public Member Functions | List of all members
Tag Class Reference

#include <tag.h>

Classes

class  Attribute

Public Types

typedef std::list< Attribute * > AttributeList

Public Member Functions

 Tag (const std::string &name, const std::string &cdata=EmptyString)
 Tag (Tag *parent, const std::string &name, const std::string &cdata=EmptyString)
 Tag (const std::string &name, const std::string &attrib, const std::string &value)
 Tag (Tag *parent, const std::string &name, const std::string &attrib, const std::string &value)
virtual ~Tag ()
const std::string xml () const
bool setPrefix (const std::string &prefix)
const std::string & prefix () const
const std::string & prefix (const std::string &xmlns) const
bool setXmlns (const std::string &xmlns, const std::string &prefix=EmptyString)
const std::string & xmlns () const
const std::string & xmlns (const std::string &prefix) const
bool addAttribute (Attribute *attr)
bool addAttribute (const std::string &name, const std::string &value)
bool addAttribute (const std::string &name, int value)
bool addAttribute (const std::string &name, long value)
void setAttributes (const AttributeList &attributes)
void addChild (Tag *child)
void addChildCopy (const Tag *child)
bool setCData (const std::string &cdata)
bool addCData (const std::string &cdata)
const std::string & name () const
const std::string cdata () const
const AttributeListattributes () const
const TagListchildren () const
const std::string & findAttribute (const std::string &name) const
bool hasAttribute (const std::string &name, const std::string &value=EmptyString) const
TagfindChild (const std::string &name) const
TagfindChild (const std::string &name, const std::string &attr, const std::string &value=EmptyString) const
bool hasChild (const std::string &name, const std::string &attr=EmptyString, const std::string &value=EmptyString) const
TagfindChildWithAttrib (const std::string &attr, const std::string &value=EmptyString) const
bool hasChildWithAttrib (const std::string &attr, const std::string &value=EmptyString) const
TagList findChildren (const std::string &name, const std::string &xmlns=EmptyString) const
void removeChild (const std::string &name, const std::string &xmlns=EmptyString)
void removeChild (Tag *tag)
void removeAttribute (const std::string &attr, const std::string &value=EmptyString, const std::string &xmlns=EmptyString)
bool hasChildWithCData (const std::string &name, const std::string &cdata) const
Tagparent () const
Tagclone () const
const std::string findCData (const std::string &expression) const
const TagfindTag (const std::string &expression) const
ConstTagList findTagList (const std::string &expression) const
bool operator== (const Tag &right) const
bool operator!= (const Tag &right) const
 operator bool () const

Detailed Description

This is an abstraction of an XML element.

Note
Use setXmlns() to set namespaces and namespace prefixes.
Author
Jakob Schroeter js@ca.nosp@m.maya.nosp@m..net
Since
0.4

Definition at line 46 of file tag.h.

Member Typedef Documentation

typedef std::list<Attribute*> AttributeList

A list of XML element attributes.

Definition at line 187 of file tag.h.

Constructor & Destructor Documentation

Tag ( const std::string &  name,
const std::string &  cdata = EmptyString 
)

Creates a new tag with a given name (and XML character data, if given).

Parameters
nameThe name of the element.
cdataThe XML character data of the element.

Definition at line 138 of file tag.cpp.

Tag ( Tag parent,
const std::string &  name,
const std::string &  cdata = EmptyString 
)

Creates a new tag as a child tag of the given parent, with a given name (and XML character data, if given).

Parameters
parentThe parent tag.
nameThe name of the element.
cdataThe XML character data of the element.

Definition at line 149 of file tag.cpp.

Tag ( const std::string &  name,
const std::string &  attrib,
const std::string &  value 
)

Creates a new tag with a given name and an attribute.

Parameters
nameThe name of the element.
attribThe attribute name.
valueThe attribute value.

Definition at line 163 of file tag.cpp.

Tag ( Tag parent,
const std::string &  name,
const std::string &  attrib,
const std::string &  value 
)

Creates a new tag as a child tag of the given parent, with a given name and an attribute.

Parameters
parentThe parent tag.
nameThe name of the element.
attribThe attribute name.
valueThe attribute value.

Definition at line 176 of file tag.cpp.

~Tag ( )
virtual

Virtual destructor.

Definition at line 228 of file tag.cpp.

Member Function Documentation

bool addAttribute ( Attribute attr)

Use this function to add a new attribute to the tag. The Tag will become the owner of the Attribute and take care of deletion. If an Attribute with the same name already exists, it will be replaced by the new one.

Parameters
attrA pointer to the attribute to add.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.
Since
1.0
Note
Do not use this function to set XML namespaces, use setXmlns() instead.

Definition at line 353 of file tag.cpp.

bool addAttribute ( const std::string &  name,
const std::string &  value 
)

Use this function to add a new attribute to the tag.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.
Note
Do not use this function to set XML namespaces, use setXmlns() instead.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.

Definition at line 384 of file tag.cpp.

bool addAttribute ( const std::string &  name,
int  value 
)

Use this function to add a new attribute to the tag. The value is an int here.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.
Note
Do not use this function to set XML namespaces, use setXmlns() instead.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.
Since
0.8

Definition at line 392 of file tag.cpp.

bool addAttribute ( const std::string &  name,
long  value 
)

Use this function to add a new attribute to the tag. The value is a long here.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.
Note
Do not use this function to set XML namespaces, use setXmlns() instead.
Since
0.9

Definition at line 400 of file tag.cpp.

bool addCData ( const std::string &  cdata)

Adds the string to the existing XML character data for this Tag.

Parameters
cdataThe additional cdata.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.

Definition at line 476 of file tag.cpp.

void addChild ( Tag child)

Use this function to add a child node to the tag. The Tag will be owned by Tag.

Parameters
childThe node to be inserted.

Definition at line 423 of file tag.cpp.

void addChildCopy ( const Tag child)

Use this function to add a copy of the given element to the tag.

Parameters
childThe node to be inserted.
Since
0.9

Definition at line 438 of file tag.cpp.

const Tag::AttributeList & attributes ( ) const

Use this function to fetch a const list of attributes.

Returns
A constant reference to the list of attributes.

Definition at line 511 of file tag.cpp.

const std::string cdata ( ) const

Use this function to retrieve the XML character data of an element.

Returns
The cdata the element contains.

Definition at line 492 of file tag.cpp.

const TagList & children ( ) const

Use this function to fetch a const list of child elements.

Returns
A constant reference to the list of child elements.

Definition at line 505 of file tag.cpp.

Tag * clone ( ) const

This function creates a deep copy of this Tag.

Returns
An independent copy of the Tag.
Since
0.7

Definition at line 665 of file tag.cpp.

const std::string & findAttribute ( const std::string &  name) const

This function can be used to retrieve the value of a Tag's attribute.

Parameters
nameThe name of the attribute to look for.
Returns
The value of the attribute if found, an empty string otherwise.

Definition at line 584 of file tag.cpp.

const std::string findCData ( const std::string &  expression) const

Evaluates the given XPath expression and returns the result Tag's character data, if any. If more than one Tag match, only the first one's character data is returned.

Note
Currently, XPath support is somewhat limited. However, it should be useable for basic expressions. For now, see src/tests/xpath/xpath_test.cpp for supported expressions.
Parameters
expressionAn XPath expression to evaluate.
Returns
A matched Tag's character data, or the empty string.
Since
1.0

Definition at line 794 of file tag.cpp.

Tag * findChild ( const std::string &  name) const

This function finds and returns the first element within the child elements of the current tag that has a matching tag name.

Parameters
nameThe name of the element to search for.
Returns
The found Tag, or 0.

Definition at line 619 of file tag.cpp.

Tag * findChild ( const std::string &  name,
const std::string &  attr,
const std::string &  value = EmptyString 
) const

This function finds and returns the first element within the child elements of the current tag, that has a certain name, and a certain attribute with a certain value.

Parameters
nameThe name of the element to search for.
attrThe name of the attribute of the child element.
valueThe value of the attribute of the child element.
Returns
The found Tag, or 0.

Definition at line 630 of file tag.cpp.

TagList findChildren ( const std::string &  name,
const std::string &  xmlns = EmptyString 
) const

Returns a list of child tags of the current tag with the given name.

Parameters
nameThe name of the tags to look for.
xmlnsAn optional namespace to check for.
Returns
A list of tags with the given name.
Note
The tags are still linked to the current Tag and should not be deleted.
Since
0.9

Definition at line 709 of file tag.cpp.

Tag * findChildWithAttrib ( const std::string &  attr,
const std::string &  value = EmptyString 
) const

This function checks whether the Tag has a child element which posesses a given attribute with an optional value. The name of the child element does not matter.

Parameters
attrThe name of the attribute of the child element.
valueThe value of the attribute of the child element.
Returns
The child if found, 0 otherwise.

Definition at line 654 of file tag.cpp.

const Tag * findTag ( const std::string &  expression) const

Evaluates the given XPath expression and returns the result Tag. If more than one Tag match, only the first one is returned.

Note
Currently, XPath support is somewhat limited. However, it should be useable for basic expressions. For now, see src/tests/xpath/xpath_test.cpp for supported expressions.
Parameters
expressionAn XPath expression to evaluate.
Returns
A matched Tag, or 0.
Since
0.9

Definition at line 800 of file tag.cpp.

ConstTagList findTagList ( const std::string &  expression) const

Evaluates the given XPath expression and returns the matched Tags.

Note
Currently, XPath support is somewhat limited. However, it should be useable for basic expressions. For now, see src/tests/xpath/xpath_test.cpp for supported expressions.
Parameters
expressionAn XPath expression to evaluate.
Returns
A list of matched Tags, or an empty TagList.
Since
0.9

Definition at line 806 of file tag.cpp.

bool hasAttribute ( const std::string &  name,
const std::string &  value = EmptyString 
) const

Checks whether the tag has a attribute with given name and optional value.

Parameters
nameThe name of the attribute to check for.
valueThe value of the attribute to check for.
Returns
Whether the attribute exists (optionally with the given value).

Definition at line 597 of file tag.cpp.

bool hasChild ( const std::string &  name,
const std::string &  attr = EmptyString,
const std::string &  value = EmptyString 
) const

This function checks whether the Tag has a child element with a given name, and optionally this child element is checked for having a given attribute with an optional value.

Parameters
nameThe name of the child element.
attrThe name of the attribute of the child element.
valueThe value of the attribute of the child element.
Returns
True if the given child element exists, false otherwise.

Definition at line 610 of file tag.cpp.

bool hasChildWithAttrib ( const std::string &  attr,
const std::string &  value = EmptyString 
) const
inline

This function checks whether the Tag has a child element which posesses a given attribute with an optional value. The name of the child element does not matter.

Parameters
attrThe name of the attribute of the child element.
valueThe value of the attribute of the child element.
Returns
True if any such child element exists, false otherwise.

Definition at line 475 of file tag.h.

bool hasChildWithCData ( const std::string &  name,
const std::string &  cdata 
) const

This function checks whether a child element with given name exists and has XML character data that equals the given cdata string.

Parameters
nameThe name of the child element.
cdataThe character data that has to exist in the child element.
Returns
True if a child element with given cdata exists, false otherwise.

Definition at line 642 of file tag.cpp.

const std::string& name ( ) const
inline

Use this function to retrieve the name of an element.

Returns
The name of the tag.

Definition at line 394 of file tag.h.

operator bool ( ) const
inline

Returns true if the Tag is valid, false otherwise.

Definition at line 587 of file tag.h.

bool operator!= ( const Tag right) const
inline

Checks two Tags for inequality. Order of attributes and child tags does matter.

Parameters
rightThe Tag to check against the current Tag.
Since
0.9

Definition at line 582 of file tag.h.

bool operator== ( const Tag right) const

Checks two Tags for equality. Order of attributes and child tags does matter.

Parameters
rightThe Tag to check against the current Tag.
Since
0.9

Definition at line 248 of file tag.cpp.

Tag* parent ( ) const
inline

Returns the tag's parent Tag.

Returns
The Tag above the current Tag. May be 0.

Definition at line 526 of file tag.h.

const std::string& prefix ( ) const
inline

Returns the namespace prefix for this Tag, if any.

Returns
The namespace prefix.
Since
1.0

Definition at line 249 of file tag.h.

const std::string & prefix ( const std::string &  xmlns) const

Returns the namespace prefix for the given namespace.

Returns
The namespace prefix for the given namespace.
Since
1.0

Definition at line 569 of file tag.cpp.

void removeAttribute ( const std::string &  attr,
const std::string &  value = EmptyString,
const std::string &  xmlns = EmptyString 
)

Removes the attribute with the given name and optional value from this Tag.

Parameters
attrThe attribute's name.
valueThe attribute's optional value.
xmlnsAn optional namespace to check for.

Definition at line 774 of file tag.cpp.

void removeChild ( const std::string &  name,
const std::string &  xmlns = EmptyString 
)

Removes and deletes all child tags that have the given name and are, optionally, within the given namespace.

Parameters
nameThe name of the tag(s) to remove from the list of child tags.
xmlnsAn optional namespace to check for.

Definition at line 728 of file tag.cpp.

void removeChild ( Tag tag)

Removes the given Tag from the list of child Tags.

Parameters
tagThe Tag to remove from the list of child Tags.
Note
The Tag tag is not deleted.

Definition at line 754 of file tag.cpp.

void setAttributes ( const AttributeList attributes)

Sets the given attributes. Any existing attributes are lost.

Parameters
attributesThe attributes to set.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.
Note
Do not use this function to set XML namespaces, use setXmlns() instead.
Since
0.9

Definition at line 408 of file tag.cpp.

bool setCData ( const std::string &  cdata)

Sets the XML character data for this Tag.

Parameters
cdataThe new cdata.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.

Definition at line 446 of file tag.cpp.

bool setPrefix ( const std::string &  prefix)

Sets the Tag's namespace prefix.

Parameters
prefixThe namespace prefix.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.
Since
1.0

Definition at line 560 of file tag.cpp.

bool setXmlns ( const std::string &  xmlns,
const std::string &  prefix = EmptyString 
)

Sets an XML namespace with a given prefix, or the default namespace if prefix is empty.

Parameters
xmlnsThe namespace value.
prefixAn optional namespace prefix.
Returns
True if the input is valid UTF-8, false otherwise. Invalid input will be ignored.
Since
1.0

Definition at line 517 of file tag.cpp.

const std::string xml ( ) const

This function can be used to retrieve the complete XML of a tag as a string. It includes all the attributes, child nodes and character data.

Returns
The complete XML.

Definition at line 301 of file tag.cpp.

const std::string & xmlns ( ) const

Returns the namespace for this element. Namespace declarations in parent tags as well as prefixes will be taken into account.

Returns
The namespace for this element.
Since
1.0

Definition at line 538 of file tag.cpp.

const std::string & xmlns ( const std::string &  prefix) const

Returns the namespace for the given prefix, or the default namespace if prefix is empty. Namespace declarations in parent tags will be taken into account. Consider the following XML:

&lt;foo:bar xmlns:foo='foobar'/&gt;

<bar/> is in the foobar namespace, having a prefix of foo. A call to prefix() will return 'foo'. A call to xmlns( "foo" ) or xmlns( prefix() ) will return 'foobar'. A call to xmlns() will also return 'foobar' (it is a shortcut to xmlns( prefix() ).

Parameters
prefixThe namespace prefix to look up, or an empty string to fetch the default namespace.
Returns
The namespace for the given prefix, or the empty string if no such prefix exists. The default namespace if an empty prefix is given.
Since
1.0

Definition at line 543 of file tag.cpp.


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