How to output two data elements from simple XML


#1

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’]}


#2

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.


#3

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. :slight_smile:

MC

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


#4

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.


#5

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.


#6

Thanks Michael for helping me with this issue. -MC


#7

On Mar 18, 5:52 pm, Michael M. 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’] )