Module
Node¶ ↑Tasks on this page:
Use method Node.previous_sibling_node to retrieve the previous sibling:
d = REXML::Document.new('<root><a/><b/><c/></root>') b = d.root[1] # => <b/> b.previous_sibling_node # => <a/>
Use method Node.next_sibling_node to retrieve the next sibling:
d = REXML::Document.new('<root><a/><b/><c/></root>') b = d.root[1] # => <b/> b.next_sibling_node # => <c/>
Use method Node.index_in_parent to retrieve the 1-based index of this node among its siblings:
d = REXML::Document.new('<root><a/><b/><c/></root>') b = d.root[1] # => <b/> b.index_in_parent # => 2
Use method Node.each_recursive to traverse a tree of nodes recursively:
xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>' d = REXML::Document.new(xml_string) d.root.each_recursive {|node| p node }
Output:
<a> ... </> <b> ... </> <c/> <b> ... </> <c/>
Use method Node.find_first_recursive to search a tree of nodes recursively:
xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>' d = REXML::Document.new(xml_string) d.root.find_first_recursive {|node| node.name == 'c' } # => <c/>
String
¶ ↑Use method Node.to_s to represent the node as a string:
xml_string = '<root><a><b><c></c></b><b><c></c></b></a></root>' d = REXML::Document.new(xml_string) d.root.to_s # => "<root><a><b><c/></b><b><c/></b></a></root>"
Use method Node.parent? to determine whether the node is a parent; class Text derives from Node:
d = REXML::Document.new('<root><a/>text<b/>more<c/></root>') t = d.root[1] # => "text" t.parent? # => false
Class
Parent also derives from Node, but overrides this method:
p = REXML::Parent.new p.parent? # => true