rexml help

D

Daniel Berger

Hi all,

After futzing with REXML and reading over the API I'm not quite
"getting it". I have a file that looks more or less like this:

<dbrc>
<database>
<name>foo</name>
<login>john</login>
<password>blah</password>
</database>
<database>
<name>bar</name>
<login>jane</login>
<password>hello</password>
</database>
<database>
<name>foo</name>
<login>jack</login>
<password>pfft</password>
</database>
</dbrc>

Given a name, I want to be able to grab the login and password. Or,
given both a name and a login, I want to be able to find the password.
In the event I am only given a name, and there is more than one entry
with that name, I want the first entry (from a top down perspective).

What is the shortest route to accomplish this with rexml? I've been
futzing around with each, each_element, root, get_text - but I'm just
not quite getting the hang of it.

To look at all of the text I did something like this:

doc = Document.new(File.new("dbrc.xml"))

doc.elements.each("/dbrc"){ |node|
node.each_element{ |element|
element.each_element{ |e|
puts e.get_text.value
}
}
}

But, I need to narrow it down more and be able to focus in on only the
elements I need.

What's the best/quickest/easiest way to accomplish this?

Regards,

Dan
 
J

James Britt

Daniel said:
Hi all,


Given a name, I want to be able to grab the login and password. Or,
given both a name and a login, I want to be able to find the password.
In the event I am only given a name, and there is more than one entry
with that name, I want the first entry (from a top down perspective).

What is the shortest route to accomplish this with rexml? I've been
futzing around with each, each_element, root, get_text - but I'm just
not quite getting the hang of it.


Play with this::

#------------------------------------------------------------
class REXML::Document
def login_details( name )
ary = self.elements.to_a( "//name[text()='#{name}']" )
parent = ary[0].parent
login = parent.elements.to_a( "login" )[0].text
pwd = parent.elements.to_a( "password" )[0].text
[login, pwd ]
end
end

doc = REXML::Document.new( xml )

puts( doc.login_details( 'bar' ).inspect )
puts( doc.login_details( 'foo' ).inspect )

#------------------------------------------------------------


REXML + XPath is your friend. Basically, use XPath to select the
node(s) you want. Then get the parent node. Then use XPath again to
get additional child nodes from that (parent) node.


James
 
D

Daniel Berger

James Britt said:
Daniel said:
Hi all,


Given a name, I want to be able to grab the login and password. Or,
given both a name and a login, I want to be able to find the password.
In the event I am only given a name, and there is more than one entry
with that name, I want the first entry (from a top down perspective).

What is the shortest route to accomplish this with rexml? I've been
futzing around with each, each_element, root, get_text - but I'm just
not quite getting the hang of it.


Play with this::

#------------------------------------------------------------
class REXML::Document
def login_details( name )
ary = self.elements.to_a( "//name[text()='#{name}']" )
parent = ary[0].parent
login = parent.elements.to_a( "login" )[0].text
pwd = parent.elements.to_a( "password" )[0].text
[login, pwd ]
end
end

doc = REXML::Document.new( xml )

puts( doc.login_details( 'bar' ).inspect )
puts( doc.login_details( 'foo' ).inspect )

#------------------------------------------------------------


REXML + XPath is your friend. Basically, use XPath to select the
node(s) you want. Then get the parent node. Then use XPath again to
get additional child nodes from that (parent) node.


James

Thank you James.

I have a followup issue. There are some attributes that are optional,
such as "interval". If I try to use XPath in this fashion..

interval = parent.elements.to_a( "interval" )[0].text

...and the "interval" node doesn't exist, it's going to raise a
NoMethodError. I *could* just rescue and ignore that error, but that
doesn't seem very clean. Is there a better way? Or do I simply tell
people they must include the optional tags, even if they're empty?

Dan
 
J

James Britt

Daniel said:
Thank you James.

I have a followup issue. There are some attributes that are optional,
such as "interval". If I try to use XPath in this fashion..

interval = parent.elements.to_a( "interval" )[0].text

...and the "interval" node doesn't exist, it's going to raise a
NoMethodError. I *could* just rescue and ignore that error, but that
doesn't seem very clean. Is there a better way? Or do I simply tell
people they must include the optional tags, even if they're empty?

Question: How much of your code is concerned with querying this data?

If you expect to read in the file once, and then run X number of queries
against it, then you may be better off with a custom data structure. Of
course, this all depends on the size of X, the size of the data,
requirements for updating or serializing the data, and so on.

I've poked around with reading in assorted XML and running queries
against the resulting documents, and typically end up reading the XML
into some other structure better suited for certain operation, like queries.

It's often much more efficient to read in the data using a stream or
pull parser, and handing default values, optional elements/attributes,
and the like as you encounter them, then to constantly run an XPath
query on non-optimized data.

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top