Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > REXML advice - output

Reply
Thread Tools

REXML advice - output

 
 
Stuart Clarke
Guest
Posts: n/a
 
      09-16-2010
Hey all,

I would like to pick your brains about Rexml and how to report from it.
For example, I am reading an XML file using references to each XML tag
like so:

doc.root.each_element("/UserData/List/ItemInfo/Title") {|e|
report.puts "Title: #{e.text}"
}
doc.root.each_element("/UserData/List/ItemInfo/Date") {|e|
report.puts "Date: #{e.text}"
}

The 'report.puts' writes this data out to a CSV file. At present I get a
list of all the titles in the XML file followed a list of the dates.
What I need it to get the side by side in a CSV file like so

Title Date
Item1 20th Jan 2009
Item2 12th Feb 2010

Does anyone have any suggestions on a suitable workflow for this?

Many thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-16-2010
On Thu, Sep 16, 2010 at 4:42 PM, Stuart Clarke
<> wrote:
> Hey all,
>
> I would like to pick your brains about Rexml and how to report from it.
> For example, I am reading an XML file using references to each XML tag
> like so:
>
> doc.root.each_element("/UserData/List/ItemInfo/Title") {|e|
> =A0report.puts "Title: #{e.text}"
> }
> doc.root.each_element("/UserData/List/ItemInfo/Date") {|e|
> =A0report.puts "Date: #{e.text}"
> }
>
> The 'report.puts' writes this data out to a CSV file. At present I get a
> list of all the titles in the XML file followed a list of the dates.
> What I need it to get the side by side in a CSV file like so
>
> Title =A0 =A0 =A0 =A0 =A0 =A0 Date
> Item1 =A0 =A0 =A0 =A0 =A0 =A0 20th Jan 2009
> Item2 =A0 =A0 =A0 =A0 =A0 =A0 12th Feb 2010
>
> Does anyone have any suggestions on a suitable workflow for this?


Just iterate over all "ItemInfo" elements and print values from sub
elements (which you can select via a relative XPath).

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
 
Stuart Clarke
Guest
Posts: n/a
 
      09-17-2010
Robert Klemme wrote:
> On Thu, Sep 16, 2010 at 4:42 PM, Stuart Clarke
> <> wrote:
>> �report.puts "Date: #{e.text}"
>> Does anyone have any suggestions on a suitable workflow for this?

> Just iterate over all "ItemInfo" elements and print values from sub
> elements (which you can select via a relative XPath).
>
> Kind regards
>
> robert

Thanks for getting back to me. I will look into this and see how I get
on.

Thanks a lot Robert.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      09-20-2010
Robert Klemme wrote:
> On Thu, Sep 16, 2010 at 4:42 PM, Stuart Clarke
> <> wrote:
>> �report.puts "Date: #{e.text}"
>> Does anyone have any suggestions on a suitable workflow for this?

> Just iterate over all "ItemInfo" elements and print values from sub
> elements (which you can select via a relative XPath).
>
> Kind regards
>
> robert


To confirm I am following you correctly, I have now got the following:

info = doc.elements.to_a("//UserData/List/ItemInfo/")

Printing out info gives a line per line entry of all children under the
tag ItemInfo.

First of all, is this what you meant? Am I correct to assume that at
this point, you are suggesting I write this data to a CSV file stripping
off the tags with a regex or something? Is this correct?

Many thanks and apologies if I have misunderstood.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
brabuhr@gmail.com
Guest
Posts: n/a
 
      09-20-2010
I managed to mess-up clicking "Send" on Friday, so I'm trying again (-:

> Stuart Clarke wrote:
>> Robert Klemme wrote:
>>> Stuart Clarke wrote:
>>>> report.puts "Date: #{e.text}"
>>>> Does anyone have any suggestions on a suitable workflow for this?
>>>
>>> Just iterate over all "ItemInfo" elements and print values from sub
>>> elements (which you can select via a relative XPath).

>>
>> Thanks for getting back to me. I will look into this and see how I get
>> on.

>
> I pulled this out of a script I use quite a bit and hacked your XPath int=

o it:
>
> ARGV.each do |filename|
> =A0 doc =3D REXML:ocument.new( File.new( filename ) )
>
> =A0 doc.elements.each("/UserData/List/ItemInfo"){|e|
> =A0 =A0print e.elements["Title"].text, "\t"
> =A0 =A0puts e.elements["Date"].text
> =A0end
> end


 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      10-26-2010
Could anybody help me with an issue you I am having with some XML I am
reading. I am using xpath to read 2 different parts of an XML file,
which looks a lot like this

<Data>
<DoneList><Vector><Count>84</Count>
<FullItemInfo>
<Count>0</Count>
<ItemInfo>
<Title>BLAH LAH</Title>
<Id>12345</Id>
</ItemInfo>
</Vector></DoneList>
<FullItemInfo>
NEXT ITEM AS BOVE

Then I have further data, which is slightly different
<NotDoneList><Vector><Count>84</Count>
<FullItemInfo>
<Count>0</Count>
<ItemInfo>
<Title>BLAH LAH</Title>
<Id>12345</Id>
</ItemInfo>
</Vector></DoneList>
<FullItemInfo>
</Data>

As you can see, the tags are the same but the first is DoneList and the
second NotDoneList. I need to process each set seperately and each set
can contain more than 1 entry. My code to give a CSV file is

doc = REXML:ocument.new(d) #call REXML to open the XML file
#To get NotDoneList data
doc.elements.each("//NotDoneList/Vector/Count/FullItemInfo") do |e|
detail =
(
e.elements['ItemInfo/Title'].text << "," <<
e.elements['ItemInfo/Id'].text
)
puts detail
end

#To get DoneList data
doc.elements.each("//DoneList/Vector/Count/FullItemInfo") do |e|
detail =
(
e.elements['ItemInfo/Title'].text << "," <<
e.elements['ItemInfo/Id'].text
)
puts detail
end

When I run this, no data in extracted and no errors are given. In
contrast if I do
doc.elements.each("//FullItemInfo") do |e|
I am able to extract all the information for both the NotDoneList and
DoneList, however this is not what I want. I want to address each data
set separately. The eventual idea will be to produce a report of all
items in the NotDoneList and another report for those in the DoneList.
I guess I am doing something wrong but I cannot see it.

Can anyone see what I am doing wrong with this? I would really
appreciate any help as I cannot figure it out.

Many thanks

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
brabuhr@gmail.com
Guest
Posts: n/a
 
      10-27-2010
> <Data>
> <DoneList><Vector><Count>84</Count>
> <FullItemInfo>
> <Count>0</Count>
> <ItemInfo>
> <Title>BLAH LAH</Title>
> <Id>12345</Id>
> </ItemInfo>
> </Vector></DoneList>
> <FullItemInfo>
> NEXT ITEM AS BOVE


Data
DoneList
Vector
Count /Count
FullItemInfo
Count /Count
ItemInfo
Title /Title
Id /Id
/ItemInfo
/Vector
/DoneList
FullItemInfo

The XML example you provided seems to have mismatched tags?

> Then I have further data, which is slightly different
> <NotDoneList><Vector><Count>84</Count>
> <FullItemInfo>
> <Count>0</Count>
> <ItemInfo>
> <Title>BLAH LAH</Title>
> <Id>12345</Id>
> </ItemInfo>
> </Vector></DoneList>
> <FullItemInfo>
> </Data>


NotDoneList
Vector
Count /Count
FullItemInfo
Count /Count
ItemInfo
Title /Title
Id /Id
/ItemInfo
/Vector
/DoneList
FullItemInfo
/Data

> doc.elements.each("//NotDoneList/Vector/Count/FullItemInfo")
> doc.elements.each("//DoneList/Vector/Count/FullItemInfo") do |e|


Can you verify and re-post a clean XML snippet? (That may help debug
your XPath.) I'm going to guess:

<Data>
<DoneList>
<Vector>
<Count/>
<FullItemInfo/>
</Vector>
</DoneList>
</Data>

In which case, the XPath might be: '//DoneList/Vector/FullItemInfo'?

 
Reply With Quote
 
Stuart Clarke
Guest
Posts: n/a
 
      10-29-2010
My issue was due to the mis matched tags actually, it was a broken XML
file.

Thanks for identifying that.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
REXML::Element.write is deprecated. See REXML::Formatters Phlip Ruby 0 01-15-2008 08:23 PM
Small issue with REXML output Chris Large Ruby 1 12-23-2005 05:28 AM
rexml error - REXML::Validation Daniel Berger Ruby 2 10-12-2004 04:19 PM
Setting the output encoding in REXML Francis Hwang Ruby 0 10-07-2004 09:16 PM
soap4r 1.4.8.1 with REXML 2.7.1 - no REXML::VERSION_MAJOR Damphyr Ruby 2 07-16-2003 09:49 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57