On Thu, Jun 14, 2012 at 7:53 PM, cyber c. <lists / ruby-forum.com> wrote:
>
> Hi,
>
> I know that i can use "Nokogiri" to parse xml files and search for
> results.
> How do we deal with xml dict files where you have a [key,string] pairs
> associated.
>
> Sample file
> <? xml version=3D"1.0" enc..... ?>
>
> <dict>
> =A0 =A0 =A0 <key>A</key>
> =A0 =A0 =A0 <string>val1</string>
> =A0 =A0 =A0 <key>B</key>
> =A0 =A0 =A0 <string>val2</string>
> =A0 =A0 =A0 <key>C</key>
> =A0 =A0 =A0 <string>val3</string>
> </dict>

08:55:20 Temp$ ./xml.rb
A -> val1
B -> val2
C -> val3
08:55:36 Temp$ cat -n xml.rb
     1  #!/opt/bin/ruby19
     2
     3  require 'nokogiri'
     4
     5  dom =3D Nokogiri.XML <<XML
     6
     7  <dict>
     8        <key>A</key>
     9        <string>val1</string>
    10        <key>B</key>
    11        <string>val2</string>
    12        <key>C</key>
    13        <string>val3</string>
    14  </dict>
    15
    16  XML
    17
    18  dom.xpath('//key').each do |key|
    19    printf "%s -> %s\n", key.text(),
key.at_xpath('following-sibling::string/text()').text()
    20  end

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/