我有这个XML文档,我想使用REXML查找特定的GitHubCommiter。我为什么要这样做呢?
<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>我试过了:
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但我找不到一种方法。有什么想法吗?
发布于 2010-10-27 01:46:53
docs say for elements (我的重点):
[]( index, name=nil)提取一个子元素。只过滤元素的子项,而不管XPath是否匹配。
搜索参数index:。这可以是一个整数,它将用于查找索引的第一个子元素,也可以是一个XPath,它将用于搜索该元素。
所以它需要是XPath:
root.elements["./GitHubCommiter[@id = '{github_user_name}']"]等。
https://stackoverflow.com/questions/4026332
复制相似问题