# How to output two data elements from simple XML

**URL:** <https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272>\
**Category:** Ruby\
**Created:** [March 18, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272 "2009-03-18T21:32:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 18, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/1 "2009-03-18T21:32:05Z")

</div>

Hi

I don’t understand how to output the exact data that I need from my xml  
document. I only need to get 2 data elements per xml segment, however I  
am getting the whole xml segment. Will you show me how to do this?

- 

I am getting this:  
\<Field FieldId=“11” Field1=“ACC\_DATE” Path="/Docs/Animal/Bird"  
…

This is what I need the output to look like:  
Cat Fur  
Dog Fur  
Bird Feather

#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
require ‘rexml/document’  
include REXML  
f = File.new(“AnimalXML.xml”)  
doc = Document.new(f)

#below ex. outputs the whole xml segment  
fields = XPath.match(doc, “//Field FieldId”)  
puts fields

#below ex. does same as above, shows whole xml segment  
doc.elements.each("\*/Field FieldId") { |element| puts  
element.attribute[‘Path’]}

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 18, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/2 "2009-03-18T23:26:28Z")

</div>

Mmcolli00 Mom wrote:

> \<Field FieldId=“10” Field1=“ACC” Path="/Docs/Animail/Dog"  
> Cat Fur  
> fields = XPath.match(doc, “//Field FieldId”)  
> puts fields
> 
> #below ex. does same as above, shows whole xml segment  
> doc.elements.each("\*/Field FieldId") { |element| puts  
> element.attribute[‘Path’]}

Call me suspicious, but are you trying to get us to do your homework for  
you?

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 19, 2009, 12:20am UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/3 "2009-03-19T00:20:42Z")

</div>

Michael I am not going to call you suspicious. But yes and no. The xml  
is not animals…it actually all numbers and letters which are  
confidential and lengthy at best so…I didn’t want to use them in this  
post for that reason. Yes I am a beginner so that’s probably another  
reason you think this may be homework.

I do not know how to go about drilling down to that last element values  
so thats the yes part. Thanks though. 🙂

MC

> Call me suspicious, but are you trying to get us to do your homework for  
> you?

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 19, 2009, 12:55am UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/4 "2009-03-19T00:55:38Z")

</div>

Sorry, that was a terrible first answer. Righto, here goes:

require ‘rexml/document’  
include REXML  
f = File.new(“AnimalXML.xml”)  
doc = Document.new(f)

#below ex. outputs the whole xml segment  
XPath.match(doc, “//Field FieldId”).each do |e|  
puts “#{e.attribute(‘Path’).to\_s.gsub!(’/Docs/Animal/’, ‘’)}  
#{e.attribute(‘Attribute’)}”  
end

Yes, removing /Docs/Animal/ like that isn’t a brilliant idea, but it  
works.

Enjoy,  
Michael

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 19, 2009, 12:34am UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/5 "2009-03-19T00:34:33Z")

</div>

Mmcolli00 Mom wrote:

> \<Field FieldId=“10” Field1=“ACC” Path="/Docs/Animail/Dog"  
> Cat Fur  
> fields = XPath.match(doc, “//Field FieldId”)  
> puts fields
> 
> #below ex. does same as above, shows whole xml segment  
> doc.elements.each("\*/Field FieldId") { |element| puts  
> element.attribute[‘Path’]}

in that case, try using parentheses, instead of brackets, like so:

doc.elements.each("\*/Field FieldId") { |element| puts  
element.attribute(‘Path’)}

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 19, 2009, 2:00am UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/6 "2009-03-19T02:00:46Z")

</div>

Thanks Michael for helping me with this issue. -MC

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [March 19, 2009, 5:50am UTC](https://www.ruby-forum.com/t/how-to-output-two-data-elements-from-simple-xml/163272/7 "2009-03-19T05:50:58Z")

</div>

On Mar 18, 5:52 pm, Michael M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> XPath.match(doc, “//Field FieldId”).each do |e|  
> puts “#{e.attribute(‘Path’).to\_s.gsub!(‘/Docs/Animal/’, ‘’)}  
> #{e.attribute(‘Attribute’)}”  
> end
> 
> Yes, removing /Docs/Animal/ like that isn’t a brilliant idea, but it works.

Perhaps slightly better:  
last\_path\_entry = e.attribute(‘Path’).value[%r{[^/]+$} ]  
or:  
last\_path\_entry = e.attributes[‘Path’][%r{[^/]+$} ]  
or, if it really can be treated as a file path:  
last\_path\_entry = File.basename( e.attributes[‘Path’] )
