简体   繁体   English

使用REXML解析XML

[英]Parsing XML with REXML

I have this XML document and I want to find an specific GitHubCommiter using REXML. 我有这个XML文档,我想使用REXML查找特定的GitHubCommiter。 Hoy do I do that? y,我这样做吗?

<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? 任何想法?

The docs say for elements (emphasis mine): 文档对elements (重点是我的):

[]( index, name=nil) Fetches a child element. []( index, name=nil)获取一个子元素。 Filters only Element children, regardless of the XPath match. 无论XPath匹配如何,仅过滤Element子元素。

index: the search parameter. index:搜索参数。 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. 这可以是一个整数(用于查找索引的第一个子元素), 或者是一个XPath(用于搜索该元素)。

So it needs to be XPath: 因此它必须是XPath:

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

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM