tags:

views:

19

answers:

1

I have this XML document and I want to find an specific GitHubCommiter using REXML. Hoy do I do that?

<users>
 <GitHubCommiter id="Nerian">
  <username>name</username>
  <password>12345</password>
 </GitHubCommiter>

 <GitHubCommiter id="xmawet">
  <username>name</username>
  <password>12345</password>
 </GitHubCommiter>

 <GitHubCommiter id="JulienChristophe">
  <username>name</username>
  <password>12345</password>
  </GitHubCommiter>
</users>

I have tried:

log = REXML::Document.new(file)                                                      
root = log.root                                                                         username = root.elements["GitHubCommiter['#{github_user_name}']"].elements['username'].text      
password =     root.elements["GitHubCommiter['#{github_user_name}']"].elements['password'].text
root.elements["GitHubCommiter['id'=>'#{github_user_name}']"].text

But I don't find a way to do it. Any idea?

+1  A: 

The docs say for elements (emphasis mine):

[]( index, name=nil) Fetches a child element. Filters only Element children, regardless of the XPath match.

index: the search parameter. This is either an Integer, which will be used to find the index‘th child Element, or an XPath, which will be used to search for the Element.

So it needs to be XPath:

root.elements["./GitHubCommiter[@id = '{github_user_name}']"]

etc.

Tomalak
That's it! Thank you!
Nerian
@Nerian: You're welcome. ;-)
Tomalak