皆さん、
サンプル XML ファイルに REXML を使用しています。
<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")
今、フランスの顧客のユーザー名を取得したいので、これを試しました:
@dataarr[@name=fenchcustomer].elements["username"].attributes["name"]
これは失敗します。たとえば、配列インデックスを使用したくありません
@dataarr[1].elements["username"].attributes["name"]
うまくいきますが、私はそれをしたくありません。ここに欠けているものがありますか。配列を使用して、アカウント名を使用してフランス人ユーザーのユーザー名を取得したいと考えています。
どうもありがとう。