Prettifying Builder::XmlMarkup.new

I’ve got a web service which outputs XML. I’m generating it very
conveniently by creating a file in the view named index.xml.builder.
That all works great, almost.

I can write that file very simply and cleanly like this:

xml.instruct!
xml.search do
xml.total @results[:total]
end

That’s really maintainable, but the output contains lots of extra
spaces and newlines that muck it up. I can get rid of all the spaces
by adding this line to the top of each xml.builder file:

xml = Builder::XmlMarkup.new

But that takes out ALL the spaces and newlines, so the entire output
is on one line. What I want is something in the middle. Through
experimentation, I’ve explicitly added newlines and spaces where I
want them, but suddenly the file is no longer as easily maintainable:

xml = Builder::XmlMarkup.new
xml.instruct!
xml.text!("\n")
xml.search do
xml.text!("\n ")
xml.total @results[:total]
end

Does anyone know of a cleaner approach where I get the maintainability
of the top example with the output of the bottom example?

Thanks!

On Jun 15, 2011, at 11:38 AM, Paul wrote:

xml = Builder::XmlMarkup.new
xml.instruct!
xml.text!("\n")
xml.search do
xml.text!("\n ")
xml.total @results[:total]
end

Does anyone know of a cleaner approach where I get the maintainability
of the top example with the output of the bottom example?

Couldn’t you run the output through pp?

Walter

On Wed, Jun 15, 2011 at 12:02 PM, Walter Lee D. [email protected]
wrote:

xml.search do
is on one line. What I want is something in the middle. Through

Does anyone know of a cleaner approach where I get the maintainability
of the top example with the output of the bottom example?

Couldn’t you run the output through pp?

Walter

Maybe if I knew how. I’m not sure how to insert pp after the view
template.

My controllers look like this:

respond_to do |format|
format.html
format.xml
end

And I don’t understand the magic enough to know how to put pp in there.

Also, I was looking at the documentation for pp and it’s not clear
what it will do with a string in xml format. If I could solve the
first problem I’ll experiment with it.

Try xml = Builder::XmlMarkup.new(:indent=>2)

Thanks. That’s where I started. That does two things: it indents the
markup just like I want, but it does a second thing, too. It leaves
all the white space that exists in the xml.builder file also, so that
the resultant XML document is really ugly and contains extra newlines
and extra spaces.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs