# REXML advice - output

**URL:** <https://rubytalk.org/t/rexml-advice-output/60086>\
**Category:** ruby-talk\
**Created:** [16 September 2010 14:42 UTC](https://rubytalk.org/t/rexml-advice-output/60086 "2010-09-16T14:42:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_Clarke](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Stuart\_Clarke](https://rubytalk.org/u/Stuart_Clarke)\
**Post date:** [16 September 2010 14:42 UTC](https://rubytalk.org/t/rexml-advice-output/60086/1 "2010-09-16T14:42:17Z")

</div>

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|  
&nbsp;&nbsp;report.puts "Title: #{e.text}"  
}  
doc.root.each\_element("/UserData/List/ItemInfo/Date") {|e|  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [16 September 2010 15:13 UTC](https://rubytalk.org/t/rexml-advice-output/60086/2 "2010-09-16T15:13:21Z")

</div>

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

Kind regards

robert

> **···**
>
> On Thu, Sep 16, 2010 at 4:42 PM, Stuart Clarke \<stuart.clarke1986@gmail.com\> 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|  
> > 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?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Stuart\_Clarke](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Stuart\_Clarke](https://rubytalk.org/u/Stuart_Clarke)\
**Post date:** [26 October 2010 21:34 UTC](https://rubytalk.org/t/rexml-advice-output/60086/3 "2010-10-26T21:34:50Z")

</div>

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::Document.new(d) #call REXML to open the XML file  
#To get NotDoneList data  
doc.elements.each("//NotDoneList/Vector/Count/FullItemInfo") do |e|  
&nbsp;&nbsp;detail =  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(  
&nbsp;&nbsp;e.elements['ItemInfo/Title'].text \<\< "," \<\<  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.elements['ItemInfo/Id'].text  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts detail  
end

#To get DoneList data  
doc.elements.each("//DoneList/Vector/Count/FullItemInfo") do |e|  
&nbsp;&nbsp;detail =  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(  
&nbsp;&nbsp;e.elements['ItemInfo/Title'].text \<\< "," \<\<  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.elements['ItemInfo/Id'].text  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stuart\_Clarke](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Stuart\_Clarke](https://rubytalk.org/u/Stuart_Clarke)\
**Post date:** [29 October 2010 20:17 UTC](https://rubytalk.org/t/rexml-advice-output/60086/4 "2010-10-29T20:17:49Z")

</div>

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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stuart\_Clarke](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Stuart\_Clarke](https://rubytalk.org/u/Stuart_Clarke)\
**Post date:** [17 September 2010 13:50 UTC](https://rubytalk.org/t/rexml-advice-output/60086/5 "2010-09-17T13:50:28Z")

</div>

Robert Klemme wrote:

> **···**
>
> > On Thu, Sep 16, 2010 at 4:42 PM, Stuart Clarke \> \<stuart.clarke1986@gmail.com\> 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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Stuart\_Clarke](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Stuart\_Clarke](https://rubytalk.org/u/Stuart_Clarke)\
**Post date:** [20 September 2010 15:19 UTC](https://rubytalk.org/t/rexml-advice-output/60086/6 "2010-09-20T15:19:36Z")

</div>

Robert Klemme 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.

> **···**
>
> > On Thu, Sep 16, 2010 at 4:42 PM, Stuart Clarke \> \<stuart.clarke1986@gmail.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [27 October 2010 00:25 UTC](https://rubytalk.org/t/rexml-advice-output/60086/7 "2010-10-27T00:25:07Z")

</div>

> \<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  
&nbsp;&nbsp;&nbsp;&nbsp;DoneList  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Vector  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Count /Count  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FullItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Count /Count  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title /Title  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id /Id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/Vector  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/DoneList  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;Vector  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Count /Count  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FullItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Count /Count  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title /Title  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Id /Id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/Vector  
&nbsp;&nbsp;&nbsp;&nbsp;/DoneList  
&nbsp;&nbsp;&nbsp;&nbsp;FullItemInfo  
&nbsp;&nbsp;&nbsp;&nbsp;/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:

&nbsp;&nbsp;&nbsp;&nbsp;\<Data\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<DoneList\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<Vector\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<Count/\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<FullItemInfo/\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</Vector\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</DoneList\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</Data\>

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

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [20 September 2010 15:35 UTC](https://rubytalk.org/t/rexml-advice-output/60086/8 "2010-09-20T15:35:49Z")

</div>

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 into it:
> > 
> > ARGV.each do |filename|  
> > doc = REXML::Document.new( File.new( filename ) )
> > 
> > doc.elements.each("/UserData/List/ItemInfo"){|e|  
> > print e.elements["Title"].text, "\t"  
> > puts e.elements["Date"].text  
> > end  
> > end
