Output an array as XML

C

Chris Gallagher

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://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"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.google.com</loc>

</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.dtd"
@sitemap.urlset("xmlns" =>
"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
 
D

Dustin Barker

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

urls = ["http://www.google.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"

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

it becomes:

@sitemap.urlset("xmlns" => "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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top