[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Merge XML documents with Ruby

Harm

9/26/2008 6:04:00 PM

Dear group,

Currently I'm working with Ruby to merge several XML documents into
one. I'm using Builder to construct the 'master' XML file:

x = Builder::XmlMarkup.new(:indent => 2)
x.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
result = x.header do |xml|
some_hash.each do |key, values|
xml.company(key)
values.each do |value|
xml.department(value.to_xml)
end
end
end

However on the spot where I write 'xml.department' trouble strikes.
The 'value' already responds to the to_xml method and generates
perfectly fine XML. I'd like to insert that XML into the master file.
If I do it the way displayed above the department tag appears twice
(once from the xml.department call and once it is used as the header
tag of the department.to_xml result). Moreover the instruct tag is
inserted and all < and > characters are escaped.

Is there anyway of doing this gracefully?
Any help is greatly appreciated.

With kind regards,
Harm
2 Answers

Mark Thomas

9/26/2008 7:52:00 PM

0

On Sep 26, 2:04 pm, Harm <harmaa...@gmail.com> wrote:
> Dear group,
>
> Currently I'm working with Ruby to merge several XML documents into
> one. I'm using Builder to construct the 'master' XML file:
>
>     x = Builder::XmlMarkup.new(:indent => 2)
>     x.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
>     result = x.header do |xml|
>       some_hash.each do |key, values|
>         xml.company(key)
>         values.each do |value|
>           xml.department(value.to_xml)
>         end
>       end
>     end
>
> However on the spot where I write 'xml.department' trouble strikes.
> The 'value' already responds to the to_xml method and generates
> perfectly fine XML.

You didn't give us much to go on. What objects does the some_hash
contain? Do you have sample output?

-- Mark.

Dejan Dimic

9/26/2008 8:12:00 PM

0

On Sep 26, 9:51 pm, Mark Thomas <m...@thomaszone.com> wrote:
> On Sep 26, 2:04 pm, Harm <harmaa...@gmail.com> wrote:
>
>
>
> > Dear group,
>
> > Currently I'm working with Ruby to merge several XML documents into
> > one. I'm using Builder to construct the 'master' XML file:
>
> >     x = Builder::XmlMarkup.new(:indent => 2)
> >     x.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
> >     result = x.header do |xml|
> >       some_hash.each do |key, values|
> >         xml.company(key)
> >         values.each do |value|
> >           xml.department(value.to_xml)
> >         end
> >       end
> >     end
>
> > However on the spot where I write 'xml.department' trouble strikes.
> > The 'value' already responds to the to_xml method and generates
> > perfectly fine XML.
>
> You didn't give us much to go on. What objects does the some_hash
> contain? Do you have sample output?
>
> -- Mark.

Have you try the hpricot (http://code.whytheluckystiff.ne...) ?

Generate your xml snippets and then merge them in one final valid xml.

Nevertheless, it would be useful to have more details.