class Element
Extends Parent
Includes Namespace
Represents a tagged XML element. Elements are characterized by having children, attributes, and names, and can themselves be children.
Attributes
Methods
Attributes
attributes (reader)
Mechanisms for accessing attributes and child elements of this element.
elements (reader)
Mechanisms for accessing attributes and child elements of this element.
Methods
add_attribute
Arguments
key
can be either an Attribute or a String. If an Attribute, the attribute is added to the list of Element attributes. If String, the argument is used as the name of the new attribute, and the value parameter must be supplied.
value<nil>
not required, and is ignored if the first argument is an Attribute. Otherwise, this is a String, and is used as the value of the new Attribute.
Returns
the Attribute added
Adds an attribute to this element, overwriting any existing attribute by the same name.
add_element
Arguments
element<nil>
optional. If Element, the element is added. Otherwise, a new Element is constructed with the argument (see Element.initialize).
attrs<nil>
If supplied, must be a hash of String name,value pairs, which will be used to set the attributes of the new Element.
Returns
the Element that was added
Adds a child to this element, optionally setting attributes in the element.
add_namespace
Arguments
prefix
the namespace. If the second argument is not supplied, this should be the URI of the default namespace.
Adds a namespace to this element
add_text
Arguments
text
if a String, a new Text instance is created and added to the parent. If Text, the object is added directly.
Returns
self. NOTE that contrary to most REXML methods, the object added to the parent is not returned.
A helper method to add a Text child. Actual Text instances can be added with regular Parent methods, such as add() and <<()
delete_attribute
Arguments
key
either an Attribute or a String. In either case, the attribute is found by matching the attribute name to the argument, and then removed. If no attribute is found, no action is taken.
Returns
the attribute removed, or nil if this Element did not contain a matching attribute
Removes an attribute
delete_element
Arguments
element
Must be an Element, String, or Integer. If Element, the element is removed. If String, the element is found (via XPath) and removed. NOTE that this means that any parent can remove any descendant. If Integer, the Element indexed by that number will be removed.
Returns
the element that was removed.
Deletes a child element.
each_element_with_attribute
Arguments
key
the name of the attribute to search for
value
the value of the attribute
max<0>
(optional) causes this method to return after yielding for this number of matching children
name<nil>
(optional) if supplied, this is an XPath that filters the children to check.
Iterates through the children, yielding for each Element that has a particular attribute set.
each_element_with_text
Arguments
max<0>
(optional) causes this method to return after yielding for this number of matching children
name<nil>
(optional) if supplied, this is an XPath that filters the children to check.
Iterates through the children, yielding for each Element that has a particular text set. @see text
get_elements
Arguments
xpath
any XPath by which to search for elements in the tree
Returns
an array of Elements that match the supplied path
This is a little slower than calling elements.each directly.
get_text
Arguments
Returns
the first Text child encountered.
This is the same method as text(), only this method returns the actual Text object, rather than the String content.
has_attributes
Returns
true if this element has any attributes set, false otherwise.
has_elements
Returns
true if this element has at least one child Element
has_text
Returns
true if this element has at least one Text child
initialize
Arguments
arg<UNDEFINED>
if not supplied, will be set to the default value. If a String, the name of this object will be set to the argument. If an Element, the object will be shallowly cloned; name, attributes, and namespaces will be copied. If a Source, the source will be scanned and parsed for an Element, and all child elements will be recursively parsed as well.
parent<nil>
if supplied, must be a Parent, and will be used as the parent of this object.
Constructor
namespace
Arguments
Returns
the URI for a namespace, or the empty string if no such namespace is declared
next_element
Returns the next sibling that is an element, or nil if there is no Element sibling after this one
prefixes
Returns
an Array of all defined namespaces
previous_element
Returns the previous sibling that is an element, or nil if there is no Element sibling prior to this one
root
Returns
the root of the document that this element belongs to. If this element doesn't belong to a document, but does belong to another Element, the parent's root will be returned, until the earliest ancestor is found.
text
Arguments
Returns
the String content of the first child text element encountered.
A convenience method which returns the first child text element. NOTE that an element may have multiple Text elements, perhaps separated by other children; consider: "<p>some text <b>this is bold!</b> more text<p>" The element <p> has two text elements, "some text " and " more text". This method would return only the first, "some text ".
text=
Arguments
text
If a String, a new Text child is created and added to this Element as the first Text child. If Text, the text is set as the first Child element. If nil, then any existing first Text child is removed.
Returns
self. NOTE that contrary to most REXML methods, the replaced content is not returned.
Sets the first Text child of this object. See text() for a discussion about Text children. If a Text child already exists, the child is replaced by this content. This means that Text content can be deleted by calling this method with a nil argument. In this case, the next Text child becomes the first Text child. In no case is the order of any siblings disturbed.
write
Arguments
writer
A String or IO (or any object supporting <<(String))
indent<0>
if supplied, must be an Integer, which is then used to determine the indentation of this element and its children.
Writes out this element, and recursively, all children.