Class: REXML::Element

Class REXML::Element |
In: | /home/ser/Work/rexml/rexml/element.rb
Parent: | Parent

Represents a tagged XML element. Elements are characterized by having children, attributes, and names, and can themselves be children.

Methods

add_attribute add_attributes add_element add_namespace add_text clone delete_attribute delete_element delete_namespace each_element each_element_with_attribute each_element_with_text get_elements get_text has_attributes? has_elements? has_text? namespace new next_element parse_stream prefixes previous_element raw root text text= whitespace write

Attributes

attributes | [R] |
context | [RW] |
elements | [R] |

Included modules

Namespace

Public Class methods

new( arg = UNDEFINED, parent=nil, context=nil )

Constructor

arg: | 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: | if supplied, must be a Parent, and will be used as the parent of this object.
context: | If supplied, must be a hash containing context items. Context items include:
            * :respect_whitespace:: the value of this is +:all+ or an array of
                    strings being the names of the elements to respect
                    whitespace for.  Defaults to +:all+.
            * :ignore_whitespace:: the value can be +:all+ or an array of
                    strings being the names of the elements to ignore whitespace on.
                    Overrides :respect_whitespace.
            * :raw:: can be +:all+, or an array of strings being the names of
                    the elements to process in raw mode.  In raw mode, special
                    characters in text is not converted to or from entities.

parse_stream(source, listener)

Public Instance methods

clone()

Creates a shallow copy of self.

      d = Document.new "<a><b/><b/><c><d/></c></a>"
      new_a = d.root.clone
      puts new_a  # => "<a/>"

root()

Evaluates to the root element 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.

whitespace()

Evaluates to `true` if whitespace is respected for this element. This is the case if the context has +:respect_whitespace+ set to +:all+ or an array containing the name of this element, and +:ignore_whitespace+ isn't set to +:all+ or an array containing the name of this element. The evaluation is tested against `expanded_name`, and so is namespace sensitive.

raw()

Evaluates to `true` if raw mode is set for this element. This is the case if the context has +:raw+ set to +:all+ or an array containing the name of this element. The evaluation is tested against `expanded_name`, and so is namespace sensitive.

prefixes()

Evaluates to an `Array` containing the prefixes (names) of all defined namespaces

namespace(prefix=nil)

Evalutas to the URI for a prefix, or the empty string if no such namespace is declared for this element. Evaluates recursively for ancestors.

add_namespace( prefix, uri=nil )

Adds a namespace to this element.

                  a = Element.new("a")
                  a.add_namespace("foo", "bar")
                  a.add_namespace(nil, "twiddle")
                  puts a   #-> <a xmlns:foo='bar' xmlns='twiddle'/>

delete_namespace(namespace="xmlns")

NEEDS DOCUMENTATION

add_element(element=nil, attrs=nil)

Adds a child to this element, optionally setting attributes in the element. @param element optional. If Element, the element is added. Otherwise, a new Element is constructed with the argument (see Element.initialize). @param attrs If supplied, must be a hash of String name,value pairs, which will be used to set the attributes of the new Element. @return the Element that was added

delete_element(element)

Deletes a child element. @param 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. @return the element that was removed.

has_elements?()

@return true if this element has at least one child Element

each_element_with_attribute( key, value, max=0, name=nil, &block )

Iterates through the children, yielding for each Element that has a particular attribute set. @param key the name of the attribute to search for @param value the value of the attribute @param max (optional) causes this method to return after yielding for this number of matching children @param name (optional) if supplied, this is an XPath that filters the children to check.

each_element_with_text( text, max=0, name=nil, &block )

Iterates through the children, yielding for each Element that has a particular text set. @param key the name of the attribute to search for @param value the value of the attribute @param max (optional) causes this method to return after yielding for this number of matching children @param name (optional) if supplied, this is an XPath that filters the children to check. @see text

each_element( xpath=nil, &block )

Synonym for elements.each

get_elements( xpath )

This is a little slower than calling elements.each directly. @return an array of Elements that match the supplied path @param xpath any XPath by which to search for elements in the tree

next_element()

Returns the next sibling that is an element, or nil if there is no Element sibling after this one

previous_element()

Returns the previous sibling that is an element, or nil if there is no Element sibling prior to this one

has_text?()

@return true if this element has at least one Text child

text( path = nil )

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 this is bold! more text<p>" The element <p> has two text elements, "some text " and " more text". This method would return only the first, "some text ". @return the String content of the first child text element encountered.

get_text(path = nil)

This is the same method as text(), only this method returns the actual Text object, rather than the String content. @return the first Text child encountered.

text=( text )

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. @param 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. @return self. NOTE that contrary to most REXML methods, the replaced content is not returned.

add_text( text )

A helper method to add a Text child. Actual Text instances can be added with regular Parent methods, such as add() and <<() @param text if a String, a new Text instance is created and added to the parent. If Text, the object is added directly. @return self. NOTE that contrary to most REXML methods, the object added to the parent is not returned.

has_attributes?()

@return true if this element has any attributes set, false otherwise.

add_attribute( key, value=nil )

Adds an attribute to this element, overwriting any existing attribute by the same name. @param 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. @param value 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. @return the Attribute added

add_attributes(hash)

Add multiple attributes to this element. EG: el.add_attributes( {"name1"=>"value1", "name2"=>"value2"} ) el.add_attributes( [ ["name1","value1"], ["name2"=>"value2"] ] ) @p hash is either a hash, or array of arrays

delete_attribute(key)

Removes an attribute @param 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. @return the attribute removed, or nil if this Element did not contain a matching attribute

write(writer, indent=-1)

Writes out this element, and recursively, all children. @param writer A String or IO (or any object supporting <<(String)) @param indent if supplied, must be an Integer, which is then used to determine the indentation of this element and its children.