# Output an array as XML

**URL:** <https://www.ruby-forum.com/t/output-an-array-as-xml/144022>\
**Category:** Ruby\
**Created:** [July 24, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/output-an-array-as-xml/144022 "2008-07-24T18:15:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 24, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/output-an-array-as-xml/144022/1 "2008-07-24T18:15:48Z")

</div>

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.com](http://www.google.com)”, “[http://yahoo.com](http://yahoo.com)”] 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"?\> http://www.google.com 

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.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)”  
@sitemap.urlset(“xmlns” =\>  
“[www.sitemaps.org - /schemas/sitemap/0.9/](http://www.sitemaps.org/schemas/sitemap/0.9)”)

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

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 24, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/output-an-array-as-xml/144022/2 "2008-07-24T18:55:35Z")

</div>

Hi Chris,

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

urls = [“[http://www.google.com](http://www.google.com)”, “[http://yahoo.com](http://yahoo.com)”]

> @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.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)”

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

> @sitemap.urlset(“xmlns” =\>  
> “[www.sitemaps.org - /schemas/sitemap/0.9/](http://www.sitemaps.org/schemas/sitemap/0.9)”)

it becomes:

@sitemap.urlset(“xmlns” =\> “[www.sitemaps.org - /schemas/sitemap/](http://www.sitemaps.org/schemas/sitemap/)  
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
