On Thu, Jun 14, 2012 at 7:53 PM, cyber c. <[email protected]> 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="1.0" enc..... ?>
>
> <dict>
> <key>A</key>
> <string>val1</string>
> <key>B</key>
> <string>val2</string>
> <key>C</key>
> <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 = 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/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en