Processing XML

I have the following xml layout:

<?xml version="1.0" encoding="UTF-8"?>

prod1

prodid1

prod2 prodid2

I am trying to use REXML via
doc.elements.each("*/doc") { |element| puts element.attributes[“prod1”]
}

  • without success

Any ideas? Thanks

On Thu, Sep 23, 2010 at 8:50 PM, Mi Sp [email protected] wrote:

<?xml version="1.0" encoding="UTF-8"?> prod1 prodid1

I am trying to use REXML via
doc.elements.each("*/doc") { |element| puts element.attributes[“prod1”]
}

“doc” doesn’t have an attribute of prod1 (no one does actually). For
example:

REXML::Document.new(x).elements.each("*/doc"){|e| e.elements.each(“field”){|f| p f.attributes; puts f.text}}
{“name”=>name=‘id’}
prod1
{“name”=>name=‘prodId’}
prodid1
{“name”=>name=‘id’}
prod2
{“name”=>name=‘prodId’}
prodid2

unknown wrote:

On Thu, Sep 23, 2010 at 8:50 PM, Mi Sp [email protected] wrote:

<?xml version="1.0" encoding="UTF-8"?> prod1 prodid1

I am trying to use REXML via
doc.elements.each("*/doc") { |element| puts element.attributes[“prod1”]
}

“doc” doesn’t have an attribute of prod1 (no one does actually). For
example:

REXML::Document.new(x).elements.each("*/doc"){|e| e.elements.each(“field”){|f| p f.attributes; puts f.text}}
{“name”=>name=‘id’}
prod1
{“name”=>name=‘prodId’}
prodid1
{“name”=>name=‘id’}
prod2
{“name”=>name=‘prodId’}
prodid2

Thanks for the quick response. You are correct - I meant to say:
doc.elements.each("*/doc") { |element| puts
element.attributes[“prodId”]}

Any ideas on how I retrieve the field value for this attribute?

#!/usr/bin/ruby -w

require ‘rexml/document’
include REXML

file = File.open(“forum.xml”)
doc = Document.new file
root = doc.root

root.elements.each(“doc/field[@name=‘prodId’]”) do |element|
puts element.text
end

This works. Thank you!

Mi Sp wrote:

unknown wrote:

On Thu, Sep 23, 2010 at 8:50 PM, Mi Sp [email protected] wrote:

<?xml version="1.0" encoding="UTF-8"?> prod1 prodid1

I am trying to use REXML via
doc.elements.each("*/doc") { |element| puts element.attributes[“prod1”]
}

“doc” doesn’t have an attribute of prod1 (no one does actually). For
example:

REXML::Document.new(x).elements.each("*/doc"){|e| e.elements.each(“field”){|f| p f.attributes; puts f.text}}
{“name”=>name=‘id’}
prod1
{“name”=>name=‘prodId’}
prodid1
{“name”=>name=‘id’}
prod2
{“name”=>name=‘prodId’}
prodid2

Thanks for the quick response. You are correct - I meant to say:
doc.elements.each("*/doc") { |element| puts
element.attributes[“prodId”]}

Any ideas on how I retrieve the field value for this attribute?

Heh…I just registered so I could post on the roughly the same subject,
but this seems to work:

#!/usr/bin/ruby -w

require ‘rexml/document’
include REXML

file = File.open(“forum.xml”)
doc = Document.new file
root = doc.root

root.elements.each(“doc/field[@name=‘prodId’]”) do |element|
puts element.text
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs