Wednesday, November 5, 2014

Nokogiri Problem

The Ruby code below prints the attributes of the request element twice, once for the <request> and once for the </request>. I am using JRuby 1.7.13. The values in the <claim/> print only once. Is there a way to have it recognize attributes only on the opening <request> tag and not assume the same attributes on the closing tag?

<?xml version="1.0" encoding="UTF-8"?>
<requests>
    <request id="77" payee="Georgia">
       <claim claimNbr="44" adjuster="Ginnie"/>
    </request>
</requests>
require 'nokogiri'
reader = Nokogiri::XML::Reader( File.open('requests.xml') ) 
reader.each do |node|
  puts node.name 
  attributes = node.attributes.inspect
  puts attributes
end
The output of my little program is this:
request
{"id"=>"77", "payee"=>"Georgia"}
#text
{}
claim
{"claimNbr"=>"44", "adjuster"=>"Ginnie"}
#text
{}
request
{"id"=>"77", "payee"=>"Georgia"}
#text
{}

A workaround has been found. The following is printed by the alternative code below:
77
Georgia
require 'nokogiri'
doc = Nokogiri::XML(open('requests.xml')) 
puts doc.xpath("//requests/request").attribute("id")
puts doc.xpath("//requests/request").attribute("payee")