Processing XML

M

Mi Sp

I have the following xml layout:

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

<add>

<doc>

<field name="id">prod1</field>

<field name="prodId">prodid1</field>
</doc>

<doc>
<field name="id">prod2</field>
<field name="prodId">prodid2</field>
</doc>
</add>

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

- without success

Any ideas? Thanks
 
B

brabuhr

<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">prod1</field>
<field name="prodId">prodid1</field>
</doc>

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:
{"name"=>name='id'}
prod1
{"name"=>name='prodId'}
prodid1
{"name"=>name='id'}
prod2
{"name"=>name='prodId'}
prodid2
 
M

Mi Sp

unknown said:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">prod1</field>
<field name="prodId">prodid1</field>
</doc>

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:
{"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?
 
C

Childs Childs

Mi said:
unknown said:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">prod1</field>
<field name="prodId">prodid1</field>
</doc>

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
 
M

Mi Sp

#!/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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top