# XML/RSS generation

**URL:** <https://www.ruby-forum.com/t/xml-rss-generation/63499>\
**Category:** Ruby\
**Created:** [June 15, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/xml-rss-generation/63499 "2006-06-15T18:12:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Watt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@Chris\_Watt](https://www.ruby-forum.com/u/Chris_Watt)\
**Post date:** [June 15, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/xml-rss-generation/63499/1 "2006-06-15T18:12:42Z")

</div>

Hi guys,

Can anyone point em in the direction of a good tutorial/manual for  
creating xml files from within ruby?

What I want to do is take a bunch of information and output it into a  
format such as:  
\<post title="My Post Title\>  
This is the text  
Vector Info

and then save it to the server in a filename such as:  
username-timestamp.xml.

The xml file would be formated as rss as well so that rss clients can  
read it.

Thanks guys  
~Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Watt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@Chris\_Watt](https://www.ruby-forum.com/u/Chris_Watt)\
**Post date:** [June 15, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/xml-rss-generation/63499/2 "2006-06-15T18:19:55Z")

</div>

On Jun 15, 2006, at 17:09, Chris Watt wrote:

> and then save it to the server in a filename such as: username-  
> timestamp.xml.

This might be a start: [http://builder.rubyforge.org/](http://builder.rubyforge.org/)

matthew smillie.

---

<div class="post-metadata">

**Author:** ![Chris\_Watt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@Chris\_Watt](https://www.ruby-forum.com/u/Chris_Watt)\
**Post date:** [June 15, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/xml-rss-generation/63499/3 "2006-06-15T18:30:33Z")

</div>

Yea I did find that but it doesn’t answer a few of my questions (and  
as yet google hasn’t yielded anything).

I don’t know how to make ruby parse it all into a file  
(username-timestamp.xml) and how to add the extra statements to the  
vector tag (i.e., bg=“bg.png”, etc…)

They seem to be my main problems at the moment.

Also, on a related note, how do I put something into a varialbe to  
reuse it later? as php would be :  
$var=“contents of var”;  
echo “$var”;

etc.

Cheers  
~Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Watt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@Chris\_Watt](https://www.ruby-forum.com/u/Chris_Watt)\
**Post date:** [June 15, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/xml-rss-generation/63499/4 "2006-06-15T19:12:49Z")

</div>

On Jun 15, 2006, at 17:29, Chris Watt wrote:

> Yea I did find that but it doesn’t answer a few of my questions (and  
> as yet google hasn’t yielded anything).
> 
> I don’t know how to make ruby parse it all into a file  
> (username-timestamp.xml) and how to add the extra statements to the  
> vector tag (i.e., bg=“bg.png”, etc…)
> 
> They seem to be my main problems at the moment.

There’s actually an example of creating attributes (“extra  
statements”) right on the first page of the xml docs. Anyway, this  
should do something close to what you want (with some explanation):

builder = Builder::XmlMarkup.new(:indent =\> 2)

# opens the post tag

xml = builder.post(:title =\> “This is the title”) { |post|

# create the text tag and its contents inside the post tag.

post.text(“This is the text”)

# ditto, the vector tag.

post.vector(“Vector Info”, :bg =\> “bg.png”, :opacity =\> “80%”)  
}

Writing files is well-documented under the File class in the standard  
docs, which you can find online at [ruby-doc.org](http://ruby-doc.org), but this is a start:

File.new(“filename”).open { |file|  
file.write(xml)  
}

> Also, on a related note, how do I put something into a varialbe to  
> reuse it later? as php would be :  
> $var=“contents of var”;  
> echo “$var”;

Maybe you should read up on the basics:  
[http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)  
[http://poignantguide.net/ruby/](http://poignantguide.net/ruby/)

It covers the basics like how variables work pretty well, including  
interpolation in strings using the #{} construct, and has plenty on I/  
O too. It’s quite useful.

matthew smillie.
