Is it possible to generate plain-old XML using Ham

2020-06-03 02:33发布

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?

7条回答
2楼-- · 2020-06-03 03:36

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.info/docs/yardoc/file.REFERENCE.html#doctype_

查看更多
登录 后发表回答