【发布时间】:2012-02-13 21:05:17
【问题描述】:
伙计们,
我将 REXML 用于示例 XML 文件:
<Accounts title="This is the test title">
<Account name="frenchcustomer">
<username name = "frencu"/>
<password pw = "hello34"/>
<accountdn dn = "https://frenchcu.com/"/>
<exporttest name="basic">
<exportname name = "basicexport"/>
<exportterm term = "oldschool"/>
</exporttest>
</Account>
<Account name="britishcustomer">
<username name = "britishcu"/>
<password pw = "mellow34"/>
<accountdn dn = "https://britishcu.com/"/>
<exporttest name="existingsearch">
<exportname name = "largexpo"/>
<exportterm term = "greatschool"/>
</exporttest>
</Account>
</Accounts>
我正在阅读这样的 XML:
@data = (REXML::Document.new file).root
@dataarr = @@testdata.elements.to_a("//Account")
现在我想获取 frenchcustomer 的用户名,所以我尝试了这个:
@dataarr[@name=fenchcustomer].elements["username"].attributes["name"]
这个失败,我不想使用数组索引,例如
@dataarr[1].elements["username"].attributes["name"]
会起作用,但我不想这样做,我在这里缺少什么吗?我想使用数组并使用帐户名称获取法国用户的用户名。
非常感谢。
【问题讨论】:
-
我已经更新了我的答案,所以有一个示例可以在不指定索引的情况下获取名为“frenchcustomer”的元素