Represents an XML element in an XML configuration file.
XML-typed NED parameters are accessible as cXMLElement via the cPar::xmlValue() method.
cXMLElement provides readonly access to XML documents via a DOM-like API. (A full-featured DOM implementation would have been too bloated for the purpose of accessing readonly configuration files).
Features: readonly access (getter methods) only; represents only elements, attributes and text content (i.e. entities, processing instructions, comments are ignored); attributes are presented as part of an element node (not as separate attribute nodes as in DOM); mixed content model is not supported (element body cannot mix text with further elements); text is represented as a property of its enclosing element (and not as separate node as in DOM); CDATA sections are also represented as text (with the above restrictions); strings are presented in UTF-8 format (which is normal ASCII string if only characters 0x01-0x7F are used; encoding of the XML file itself may use arbitrary encoding provided it's supported by the underlying XML parser); no namespace support.
Supports XPath-like addressing via the getElementByPath() member function.
File inclusion is provided via limited support of the XInclude 1.0 spec, if the underlying XML parser supports it (e.g. Expat does not). An element <xi:include href="doc.xml"/> will be replaced with the content of the corresponding document. The "href" and "parse" attributes from the XInclude spec are supported.
Classes | |
class | ParamResolver |
Base class for classes that resolve parameters ($PARAM) that occur in in XPath expressions to their values. More... | |
Public Member Functions | |
virtual cXMLElement * | dup () const override |
Redefined cObject methods. | |
virtual std::string | str () const override |
virtual void | forEachChild (cVisitor *v) override |
Common properties | |
virtual const char * | getTagName () const |
virtual const char * | getSourceFileName () const |
virtual int | getSourceLineNumber () const |
virtual const char * | getSourceLocation () const |
virtual const char * | getNodeValue () const |
virtual const char * | getAttribute (const char *attr) const |
virtual bool | hasAttributes () const |
virtual cXMLAttributeMap | getAttributes () const |
virtual std::string | getXML () const |
Generic access to children and siblings | |
virtual cXMLElement * | getParentNode () const |
virtual bool | hasChildren () const |
virtual cXMLElement * | getFirstChild () const |
virtual cXMLElement * | getLastChild () const |
virtual cXMLElement * | getNextSibling () const |
virtual cXMLElement * | getPreviousSibling () const |
virtual cXMLElement * | getFirstChildWithTag (const char *tagname) const |
virtual cXMLElement * | getNextSiblingWithTag (const char *tagname) const |
virtual cXMLElementList | getChildren () const |
virtual cXMLElementList | getChildrenByTagName (const char *tagname) const |
virtual cXMLElementList | getElementsByTagName (const char *tagname) const |
Utility functions | |
virtual cXMLElement * | getFirstChildWithAttribute (const char *tagname, const char *attr, const char *attrvalue=nullptr) const |
virtual cXMLElement * | getElementById (const char *idattrvalue) const |
virtual cXMLElement * | getElementByPath (const char *pathexpression, cXMLElement *root=nullptr, ParamResolver *resolver=nullptr) const |
Public Member Functions inherited from cOwnedObject | |
cOwnedObject () | |
cOwnedObject (const char *name, bool namepooling=true) | |
cOwnedObject (const cOwnedObject &obj) | |
virtual | ~cOwnedObject () |
cOwnedObject & | operator= (const cOwnedObject &o) |
virtual void | parsimPack (cCommBuffer *buffer) const override |
virtual void | parsimUnpack (cCommBuffer *buffer) override |
virtual cObject * | getOwner () const override |
virtual bool | isOwnedObject () const override |
Public Member Functions inherited from cNamedObject | |
cNamedObject () | |
cNamedObject (const char *name, bool namepooling=true) | |
cNamedObject (const cNamedObject &obj) | |
virtual | ~cNamedObject () |
cNamedObject & | operator= (const cNamedObject &o) |
virtual void | setName (const char *s) |
virtual const char * | getName () const override |
virtual void | setNamePooling (bool b) |
virtual bool | getNamePooling () |
Public Member Functions inherited from cObject | |
cObject () | |
cObject (const cObject &other)=default | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
virtual const char * | getFullName () const |
virtual std::string | getFullPath () const |
virtual std::string | getClassAndFullName () const |
virtual std::string | getClassAndFullPath () const |
const cObject * | getThisPtr () const |
virtual std::ostream & | printOn (std::ostream &os) const |
virtual bool | isSoftOwner () const |
cObject * | findObject (const char *name, bool deep=true) |
virtual cClassDescriptor * | getDescriptor () const |
void | copyNotSupported () const |
Additional Inherited Members | |
Static Public Member Functions inherited from cOwnedObject | |
static long | getTotalObjectCount () |
static long | getLiveObjectCount () |
static void | resetObjectCounters () |
static cSoftOwner * | getOwningContext () |
Protected Member Functions inherited from cObject | |
virtual void | take (cOwnedObject *obj) |
virtual void | drop (cOwnedObject *obj) |
void | dropAndDelete (cOwnedObject *obj) |
|
inlineoverridevirtual |
Should be redefined in subclasses to create an exact copy of this object. The default implementation just throws an error, to indicate that the method was not redefined.
Reimplemented from cObject.
|
overridevirtual |
Returns a one-line description of the element.
Reimplemented from cObject.
|
overridevirtual |
|
inlinevirtual |
Returns the element name.
|
inlinevirtual |
Returns the file name this element originally came from.
|
inlinevirtual |
Returns the line number in the file this element originally came from.
|
virtual |
Returns a string containing a file/line position showing where this element originally came from.
|
virtual |
Returns text node in the element, or nullptr otherwise. (Mixing text and child elements is not supported.)
|
virtual |
Returns the value of the attribute with the given name. It returns nullptr if the given attribute is not found.
|
virtual |
Returns true if the element has attributes
|
virtual |
Returns attributes in an a std::map.
|
virtual |
Returns the subtree as an XML fragment.
|
virtual |
Returns the parent element, or nullptr if this element has no parent.
|
virtual |
Returns true if the element has child elements.
|
virtual |
Returns pointer to the first child element, or nullptr if this element has no children.
|
virtual |
Returns pointer to the last child element, or nullptr if this element has no children.
|
virtual |
Returns pointer to the next sibling of this element (i.e. the next child in the parent element). Returns nullptr if there're no subsequent elements.
getFirstChild() and getNextSibling() can be used to loop through the child list:
for (cXMLElement *child=element->getFirstChild(); child; child = child->getNextSibling()) { ... }
|
virtual |
Returns pointer to the previous sibling of this element (i.e. the previous child in the parent element). Returns nullptr if there're no elements before this one.
|
virtual |
Returns pointer to the first child element with the given tag name, or nullptr if this element has no such children.
|
virtual |
Returns pointer to the next sibling of this element with the given tag name. Return nullptr if there're no such subsequent elements.
getFirstChildWithTag() and getNextSiblingWithTag() are a convient way to loop through elements with a certain tag name in the child list:
for (cXMLElement *child=element->getFirstChildWithTag("foo"); child; child = child->getNextSiblingWithTag("foo")) { ... }
|
virtual |
Returns list of child elements.
|
virtual |
Returns list of child elements with the given tag name.
|
virtual |
Returns list of contained elements with the given tag name, in preorder traversal order.
|
virtual |
Returns find first child element with the given tagname and the given attribute present, and (optionally) having the given value. tagname might be nullptr – then any element with the given attribute will be accepted. Returns nullptr if not found.
|
virtual |
Returns the first element which has an "id" attribute with the given value. ("id" attributes are supposed to be unique in an XML document.) Returns nullptr if not found.
|
virtual |
Returns the first element designated by the given path expression. ("First" in the sense of preorder depth-first traversal.) Path expressions must be given in an XPath-like notation: they consist of path components (or "steps") separated by "/" or "//". A path component can be an element tag name, "*", "." or ".."; tag name and "*" can have an optional predicate in the form "[position]" or "[@attribute='value']". "/" means child element; "//" means a element any levels under the current one; ".", ".." and "*" mean what you expect them to: current element, parent element, element with any tag name.
Examples:
.
– this element./foo
– first "foo" child of this elementfoo
– same as ./foo
(initial ./
can be omitted)./foo
– first "foo" child of this element./foo/bar
– first "bar" child of first "foo" child of this element.//bar
– first "bar" anywhere under this element (depth-first search!)./ * /bar
– first "bar" child two levels below this element (remove the spaces!)./foo[0]
– first "foo" child of this element./foo[1]
– second "foo" child of this element./foo[@color='green']
– first "foo" child which has attribute "color" with value "green".//bar[1]
– a "bar" anywhere under this element which is the second "bar" among its siblings.// * [@color='yellow']
– an element anywhere under this element with attribute color="yellow" (remove the spaces!).// * [@color='yellow']/foo/bar
– first "bar" child of first "foo" child of a "yellow-colored" element (remove the spaces!)The method throws an exception if the path expression is invalid, and returns nullptr if the element is not found.