[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to output two data elements from simple XML

Mmcolli00 Mom

3/18/2009 8:29:00 PM

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']}
--
Posted via http://www.ruby-....

26 Answers

Michael Malone

3/18/2009 10:23:00 PM

0

Mmcolli00 Mom wrote:
> 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.
=======================================================================


Mmcolli00 Mom

3/18/2009 11:18:00 PM

0

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?
>
--
Posted via http://www.ruby-....

Michael Malone

3/18/2009 11:31:00 PM

0

Mmcolli00 Mom wrote:
> 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.
=======================================================================


Michael Malone

3/18/2009 11:53:00 PM

0

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


Mmcolli00 Mom

3/19/2009 12:58:00 AM

0

Thanks Michael for helping me with this issue. -MC
--
Posted via http://www.ruby-....

Phrogz

3/19/2009 4:47:00 AM

0

On Mar 18, 5:52 pm, Michael Malone <michael.mal...@tait.co.nz> 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'] )

marcuscp

7/6/2011 3:21:00 PM

0

On Jul 6, 1:30 pm, John Doherty <j...@johndoherty.com> wrote:
> On Jul 2, 4:16 pm, Fattuchus <fattuc...@yahoo.com> wrote:
>
>
>
> > Given all that I've read, I would say that John was a
> > moderate . . . . .like moi!
>
> Huh?
>
> Lennon was anything but a moderate. From 68 onward, he was strongly
> pro-feminist, to the point of staying home with Sean. At the time ,
> the idea was radical that a father would be the primary caretaker.
>
> he did fundraisers for radical groups, hung out repeatedly with
> Yippies Abbie Hoffman & Jerry Rubin, and though he later denounced
> some of the personalities, I never heard him renounce the notion of
> "Power to the People". He was the most the most famous vocal opponent
> of US militarism in Viet Nam, writing both "Give Peace a Chance" and
> "Happy Xmas/ War is Over".
>
> His song "Working Class Hero" is a blistering denunciation of
> bourgeois class values and the lies of the modern state.
>
> He wrote a song praising Angela Davis, a Communist & Black Panther.
>
> Another song was "Woman is the Nigger of the World".
>
> And from that, you get "moderate?"
>
> Or is is just the Fred Seaman report of Lennon's alleged kind words
> for the Reagan candidacy balances out the previous 15 years of left
> wing sympathies?

Let's face it, Lennon would be on the Audacity Of Hope with the Gaza
Freedom Flotilla singing "If you had the luck of the Palestinians,
you'd be sorry and wish you were dead, If you had the luck of the
Palestinians, you'd wish you were Jewish instead, yes you'd wish you
were Jewish instead. Sixty-three years of torture and hunger, drove
the people away from their land. A land full of beauty and wonder, was
raped by the Stern and Irgun."

Fattuchus

7/6/2011 3:38:00 PM

0

On Jul 5, 11:30 pm, John Doherty <j...@johndoherty.com> wrote:
> On Jul 2, 4:16 pm, Fattuchus <fattuc...@yahoo.com> wrote:
>
>
>
> > Given all that I've read, I would say that John was a
> > moderate . . . . .like moi!
>
> Huh?
>
> Lennon was anything but a moderate. From 68 onward, he was strongly
> pro-feminist, to the point of staying home with Sean. At the time ,
> the idea was radical that a father would be the primary caretaker.


Yoko stated in a published interview that when she learned she was
pregnant, she did not want the baby and wanted an abortion. John
wanted the baby. She made a deal with him: "You want a baby? You
have to take care of him and let me have control of your money and run
the family business."

I think he stayed home with Sean for a number of reasons; being a
feminist was one of them, but there were several including guilt over
the way he treated Julian.

>
> he did fundraisers for radical groups, hung out repeatedly with
> Yippies Abbie Hoffman & Jerry Rubin, and though he later denounced
> some of the personalities, I never heard him renounce the notion of
> "Power to the People".

True, but I think he moved more "toward the middle" as he got older.


He was the most the most famous vocal opponent
> of US militarism in Viet Nam, writing both "Give Peace a Chance" and
> "Happy Xmas/ War is Over".
>
> His song "Working Class Hero" is a blistering denunciation of
> bourgeois class values and the lies of the modern state.
>
> He wrote a song praising Angela Davis, a Communist & Black Panther.
>
> Another song was "Woman is the Nigger of the World".
>
> And from that, you get "moderate?"


Everything you said is true about John from the early 1970's. But I do
think he became more conservative as he aged.


Fattuchus

7/6/2011 3:41:00 PM

0

On Jul 6, 11:21 am, marcuscp <phelanmar...@gmail.com> wrote:
> On Jul 6, 1:30 pm, John Doherty <j...@johndoherty.com> wrote:
>
>
>
>
>
> > On Jul 2, 4:16 pm, Fattuchus <fattuc...@yahoo.com> wrote:
>
> > > Given all that I've read, I would say that John was a
> > > moderate . . . . .like moi!
>
> > Huh?
>
> > Lennon was anything but a moderate. From 68 onward, he was strongly
> > pro-feminist, to the point of staying home with Sean. At the time ,
> > the idea was radical that a father would be the primary caretaker.
>
> > he did fundraisers for radical groups, hung out repeatedly with
> > Yippies Abbie Hoffman & Jerry Rubin, and though he later denounced
> > some of the personalities, I never heard him renounce the notion of
> > "Power to the People". He was the most the most famous vocal opponent
> > of US militarism in Viet Nam, writing both "Give Peace a Chance" and
> > "Happy Xmas/ War is Over".
>
> > His song "Working Class Hero" is a blistering denunciation of
> > bourgeois class values and the lies of the modern state.
>
> > He wrote a song praising Angela Davis, a Communist & Black Panther.
>
> > Another song was "Woman is the Nigger of the World".
>
> > And from that, you get "moderate?"
>
> > Or is is just the Fred Seaman report of Lennon's alleged kind words
> > for the Reagan candidacy balances out the previous 15 years of left
> > wing sympathies?
>
> Let's face it, Lennon would be on the Audacity Of Hope with the Gaza
> Freedom Flotilla singing "If you had the luck of the Palestinians,
> you'd be sorry and wish you were dead, If you had the luck of the
> Palestinians, you'd wish you were Jewish instead, yes you'd wish you
> were Jewish instead. Sixty-three years of torture and hunger, drove
> the people away from their land. A land full of beauty and wonder, was
> raped by the Stern and Irgun."-

Wishful thinking. BTW, the Jewish people made the land BEAUTIFUL.

This stuff really haunts your dreams, doesn't it?

moonpie

7/6/2011 3:52:00 PM

0

On Wed, 6 Jul 2011 08:41:29 -0700 (PDT), Fattuchus
<fattuchus@yahoo.com> wrote:

>On Jul 6, 11:21?am, marcuscp <phelanmar...@gmail.com> wrote:
>> On Jul 6, 1:30?pm, John Doherty <j...@johndoherty.com> wrote:
>>
>>
>>
>>
>>
>> > On Jul 2, 4:16?pm, Fattuchus <fattuc...@yahoo.com> wrote:
>>
>> > > Given all that I've read, I would say that John was a
>> > > moderate . . . . .like moi!
>>
>> > Huh?
>>
>> > Lennon was anything but a moderate. From 68 onward, he was strongly
>> > pro-feminist, to the point of staying home with Sean. At the time ,
>> > the idea was radical that a father would be the primary caretaker.
>>
>> > he did fundraisers for radical groups, hung out repeatedly with
>> > Yippies Abbie Hoffman & Jerry Rubin, and though he later denounced
>> > some of the personalities, I never heard him renounce the notion of
>> > "Power to the People". He was the most the most famous vocal opponent
>> > of US militarism in Viet Nam, writing both "Give Peace a Chance" and
>> > "Happy Xmas/ War is Over".
>>
>> > His song "Working Class Hero" is a blistering denunciation of
>> > bourgeois class values and the lies of the modern state.
>>
>> > He wrote a song praising Angela Davis, a Communist & Black Panther.
>>
>> > Another song was "Woman is the Nigger of the World".
>>
>> > And from that, you get "moderate?"
>>
>> > Or is is just the Fred Seaman report of Lennon's alleged kind words
>> > for the Reagan candidacy balances out the previous 15 years of left
>> > wing sympathies?
>>
>> Let's face it, Lennon would be on the Audacity Of Hope with the Gaza
>> Freedom Flotilla singing "If you had the luck of the Palestinians,
>> you'd be sorry and wish you were dead, If you had the luck of the
>> Palestinians, you'd wish you were Jewish instead, yes you'd wish you
>> were Jewish instead. Sixty-three years of torture and hunger, drove
>> the people away from their land. A land full of beauty and wonder, was
>> raped by the Stern and Irgun."-
>
>Wishful thinking. BTW, the Jewish people made the land BEAUTIFUL.
>
>This stuff really haunts your dreams, doesn't it?


He's a jerk, pure and simple. I've never gotten anything from him but
flames.

Why bother....