[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Output an array as XML

Chris Gallagher

7/24/2008 4:10:00 PM

Hi,

Im currently trying to build a sitemap.xml file with ruby code.

at the moment i have an array containing urls so @urls =
["http://www.google..., "http://yahoo...] etc...

What i want to do is to loop through these and output them into the
following xml structure:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9...
<url>
<loc>http://www.google.com&l...

</url>
</urlset>


Im currently trying to do it with builder and have the following:

@sitemap = Builder::XmlMarkup.new()
@sitemap.instruct!
@sitemap.declare! :DOCTYPE, :html, :PUBLIC, "-//W3C//DTD XHTML 1.0
Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict...
@sitemap.urlset("xmlns" =>
"http://www.sitemaps.org/schemas/sitemap...)

im at the point where i need to take the @urls and insert them in
between the urlset element.

any ideas on how i might do this?

Cheers
--
Posted via http://www.ruby-....

1 Answer

Dustin Barker

7/24/2008 4:49:00 PM

0

Hi Chris,

>
> Im currently trying to do it with builder and have the following:

urls = ["http://www.google..., "http://yahoo...]

> @sitemap = Builder::XmlMarkup.new()
> @sitemap.instruct!
> @sitemap.declare! :DOCTYPE, :html, :PUBLIC, "-//W3C//DTD XHTML 1.0
> Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict...

@sitemap.urlset can take a block as a parameter:

>
> @sitemap.urlset("xmlns" =>
> "http://www.sitemaps.org/schemas/sitemap...)

it becomes:

@sitemap.urlset("xmlns" => "http://www.sitemaps.org/schema...
0.9") do |urlset|
urls.each do |loc|
urlset.url { |url| url.loc(loc) }
end
end

The block argument 'urlset' is effectively the urlset tag and behaves
the same way as @sitemap. The block that is passed to @sitemap.urlset
can iterate the array of URLs and build the child nodes (also using
blocks to build their children and so on).

-Dustin