1

皆さん、

サンプル 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"]

うまくいきますが、私はそれをしたくありません。ここに欠けているものがありますか。配列を使用して、アカウント名を使用してフランス人ユーザーのユーザー名を取得したいと考えています。

どうもありがとう。

4

3 に答える 3

3

の使用をお勧めしますXPath

最初の一致ではfirstメソッドを使用できます。配列の場合はmatch.

上記のコードは、アカウント "frenchcustomer" のユーザー名を返します。

REXML::XPath.first(yourREXMLDocument, "//Account[@name='frenchcustomer']/username/@name").value

で作成した配列を本当に使用したい場合は、 method@@testdata.elements.to_a("//Account")を使用できます。find

french_cust_elt = the_array.find { |elt| elt.attributes['name'].eql?('frenchcustomer') }
french_username = french_cust_elt.elements["username"].attributes["name"]
于 2012-02-13T21:32:14.517 に答える
1
puts @data.elements["//Account[@name='frenchcustomer']"]
      .elements["username"]
      .attributes["name"]

複数の同一の名前を反復処理する場合:

@data.elements.each("//Account[@name='frenchcustomer']") do |fc|
  puts fc.elements["username"].attributes["name"]
end
于 2012-02-13T21:31:58.837 に答える
0

私はあなたが何であるかわかりません@@testdata、私は次のテストコードで試しました:

require "rexml/document"

@data = (REXML::Document.new DATA).root 
@dataarr = @data.elements.to_a("//Account")

# Works
p @dataarr[1].elements["username"].attributes["name"]

#Works not
#~ p @dataarr[@name='fenchcustomer'].elements["username"].attributes["name"]

#@dataarr is an array
@dataarr.each{|acc|
  next unless acc.attributes['name'] =='frenchcustomer'
  p acc.elements["username"].attributes["name"]
}

#@dataarr is an array
puts "===Array#each"
@dataarr.each{|acc|
  next unless acc.attributes['name'] =='frenchcustomer'
  p acc.elements["username"].attributes["name"]
}

puts "===XPATH"
@data.elements.to_a("//Account[@name='frenchcustomer']").each{|acc|
  p acc.elements["username"].attributes["name"]
}

__END__
<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>

私は rexml にあまり詳しくないので、もっと良い解決策があると思います。しかし、誰かが私のコードを使ってより良いソリューションを構築できるかもしれません。

于 2012-02-13T21:24:23.643 に答える