Problem with REXML

Hi!

I think I have an problem understanding REXML.

let’s say I have this XML file:

<?xml version="1.0" ?> lala hans lele hens

Now I want my application to get the author of the book called “lala”.
In my perfect world something like that would work:

doc.each_element(“BOOK”) {|book| puts book.element(“AUTHOR”) if
"lala" == book.element(“TITLE”)}

or, even better:

@element = Element.new()
doc.each_element(“BOOK”) {|book| @element = book if
"lala" == book.element(“TITLE”)}

working with the documentation of REXML, it seems, that the usual way
to access a certein element is over its attributes, which is pretty
useless in my example.
And it seems impossible to get the parent of an element.

have I missed something?

regards,

Andreas.

Andreas Pinkert wrote:

Now I want my application to get the author of the book called “lala”.

quirky hack:

$:.unshift 'rexml/1.2.5/'
require 'rexml/document’
include REXML

str=
’<?xml version="1.0" ?>

lala hans lele hens '

doc = Document.new str
XPath.each doc,’//AUTHOR’ do |res|
if res.previous_sibling.previous_sibling.text == 'lala’
puts res.text
end
end

···

The above really is neither general nor elegant. Some depends on your
DTD, some on REXML.

In my perfect world something like that would work:

doc.each_element(“BOOK”) {|book| puts book.element(“AUTHOR”) if
"lala" == book.element(“TITLE”)}

In my perfect world, the whole thing would be solved with one short line
of code, and a nice XPath. Perhaps that’s possible with more recent
versions of REXML.

And it seems impossible to get the parent of an element.

.parent.

Do p element.methods:

[“whitespace”, “raw”, “add_attributes”, “each_element_with_text”,
“delete_elemen
t”, “delete_namespace”, “namespace”, “context”, “text=”, “write”,
“add_attribute
”, “next_element”, “prefixes”, “9409”, “root”, “context=”,
“has_attributes?”
, “get_text”, “get_elements”, “add_namespace”, “elements”, “add_text”,
“has_text
?”, “each_element_with_attribute”, “attributes”, “delete_attribute”,
“text”, “pr
evious_element”, “each_element”, “has_elements?”, “add_element”,
9377”, “cl
one”, “name=”, “prefix=”, “expanded_name”, “local_name”, “has_name?”,
“fully_exp
anded_name”, “prefix”, “name”, “replace_child”, “delete_at”, “each”,
“size”, “in
sert_before”, “delete”, “add”, “to_a”, “insert_after”, “[]”, “push”,
"[]=", “eac
h_child”, “each_index”, “<<”, “index”, “delete_if”, “unshift”, “min”,
“find_all”
, “entries”, “each_with_index”, “map”, “detect”, “include?”, “collect”,
“find”,
“member?”, “reject”, “grep”, “max”, “select”, “sort”,
“previous_sibling”, “next_
sibling”, “bytes”, “parent”, “previous_sibling=”, “next_sibling=”,
“document”, “
parent=”, “remove”, “replace_with”, “read_with_substitution”, “to_s”,
“previous_
sibling_node”, “write_with_substitution”, “indent”, “next_sibling_node”,
"==", “
===”, “singleton_methods”, “tainted?”, “eql?”, “methods”, “dup”, “nil?”,
“is_a?”
, “method”, “instance_of?”, “class”, “instance_variables”, “send”,
“frozen?”, “_
id_”, “send”, “=~”, “hash”, “protected_methods”, “untaint”,
“respond_to?”,
“public_methods”, “taint”, “equal?”, “kind_of?”, “inspect”, “display”,
“instanc
e_eval”, “type”, “freeze”, “id”, “extend”, “private_methods”]

Tobi


http://www.pinkjuice.com/

Hi,

···

From: Andreas Pinkert the_supernova@gmx.net
Subject: Problem with REXML
Date: Tue, 13 Aug 2002 05:48:42 +0900
Message-ID: aj967j$4br$1@piggy.rz.tu-ilmenau.de

doc.each_element(“BOOK”) {|book| puts book.element(“AUTHOR”) if
"lala" == book.element(“TITLE”)}

What about this?

REXML::XPath.each(doc,"//AUTHOR[…/TITLE/text()=‘lala’]") do |author|
puts author # This is “puts autor.text” if you want to get “hans”.
end

or, even better:

@element = Element.new()
doc.each_element(“BOOK”) {|book| @element = book if
"lala" == book.element(“TITLE”)}

And

REXML::XPath.each(doc,"//BOOK[TITLE/text()=‘lala’]") do |book|
@element = book
end

yours,

======================
SUTOU Kouhei
kou@cneti.net

Thank you both.

I just hadn’t the clue, how to work with the classes.

Your examples gave me a good hint.
I will now check out the power of XPath.

regards,

Andreas.

···


Grundgütiger! Was sind das für Viecher?

Hi,

···

From: SUTOU Kouhei kou@cneti.net
Subject: Re: Problem with REXML
Date: Tue, 13 Aug 2002 13:27:57 +0900
Message-ID: 20020813.132754.104026301.kou@cneti.net

REXML::XPath.each(doc,"//AUTHOR[…/TITLE/text()=‘lala’]") do |author|
puts author # This is “puts autor.text” if you want to get “hans”.
end

Another:

doc.root.each_element(“BOOK”) do |book|
puts book.get_text(“AUTHOR”).to_s if “lala” == book.get_text(“TITLE”).to_s
end

REXML::XPath.each(doc,"//BOOK[TITLE/text()=‘lala’]") do |book|
@element = book
end

Another:

doc.root.each_element(“BOOK”) do |book|
@element = book if “lala” == book.get_text(“TITLE”).to_s
end

yours,

======================
SUTOU Kouhei
kou@cneti.net