how to output two data elements from simple XML

M

Mmcolli00 Mom

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?

<AnimalXML>
- <FID="28">
<Field FieldId="0" Field1="" Path="/Docs/Animal/Cat" Attribute="Fur"
/>
<Field FieldId="10" Field1="ACC" Path="/Docs/Animail/Dog"
Attribute="Fur" />
<Field FieldId="11" Field1="ACC_DATE" Path="/Docs/Animal/Bird"
Attribute="Feather" />


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']}
 
M

Michael Malone

Mmcolli00 said:
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?

<AnimalXML>
- <FID="28">
<Field FieldId="0" Field1="" Path="/Docs/Animal/Cat" Attribute="Fur"
/>
<Field FieldId="10" Field1="ACC" Path="/Docs/Animail/Dog"
Attribute="Fur" />
<Field FieldId="11" Field1="ACC_DATE" Path="/Docs/Animal/Bird"
Attribute="Feather" />


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']}
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.
=======================================================================
 
M

Mmcolli00 Mom

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
 
M

Michael Malone

Mmcolli00 said:
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?

<AnimalXML>
- <FID="28">
<Field FieldId="0" Field1="" Path="/Docs/Animal/Cat" Attribute="Fur"
/>
<Field FieldId="10" Field1="ACC" Path="/Docs/Animail/Dog"
Attribute="Fur" />
<Field FieldId="11" Field1="ACC_DATE" Path="/Docs/Animal/Bird"
Attribute="Feather" />


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']}
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.
=======================================================================
 
M

Michael Malone

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.
=======================================================================
 
P

Phrogz

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'] )
 

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
474,007
Messages
2,570,266
Members
46,865
Latest member
AveryHamme

Latest Threads

Top