views:

240

answers:

7

I've been working on a piece of software where I need to generate a custom XML file to send back to a client application. The current solutions on Ruby/Rails world for generating XML files are slow, at best. Using builder or event Nokogiri, while have a nice syntax and are maintainable solutions, they consume too much time and processing.

I definetly could go to ERB, which provides a good speed at the expense of building the whole XML by hand.

HAML is a great tool, have a nice and straight-forward syntax and is fairly fast. But I'm struggling to build pure XML files using it. Which makes me wonder, is it possible at all?

Does any one have some pointers to some code or docs showing how to do this, build a full, valid XML from HAML?

+1  A: 

It should be possible. After all you can create plain old XML with Notepad.

THomasW
What is your logic? Notepad is an interactive text editor. HAML is an automated markup generator. What's the connection?
James A. Rosen
I think he's just saying that there's absolutely nothing fancy about XML whatsoever.
Matchu
+2  A: 

Maybe I'm missing something, but what would be so different about using haml for xml as opposed to html?

You say you're struggling, but what are you struggling with? Could you post some sample code?

theIV
+4  A: 
%test
  %test2 hello
  %item{:name => "blah"}

run it through haml

haml hamltest.haml test.xml

open the file in a browser

<test>
  <test2>hello</test2>
  <item name='blah'></item>
</test>

The HAML reference talks about html tags and gives some examples. HAML reference

Beanish
+1  A: 

Haml can produce XML just as easily as HTML (I've used it for FBML and XHTML). What problems are you having?

Marnen Laibow-Koser
A: 

I've not used HAML, but if you can't make it work another option is Builder.

Jordan
A: 

what about creating the xml header, e.g. <?xml version="1.0" encoding="UTF-8"?> ?

Silvia
+2  A: 

Doing XML in HAML is easy, just start off your template with:

!!! XML

which produces

<?xml version='1.0' encoding='utf-8' ?>

Then as @beanish said earlier, you "make up your own tags":

%test
  %test2 hello
  %item{:name => "blah"}

to get

<test>
  <test2>hello</test2>
  <item name='blah'></item>
</test>

More: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#doctype_

philoye