My Head Hurts
My Head Hurts

Reputation: 37675

Play 1.2.4 : Rendering XML in template

I have recently upgraded to Play 1.2.4 and I believe the bug fix for the following ticket has had a negative impact on some of my code:

https://play.lighthouseapp.com/projects/57987/tickets/549-play-rest-xml-rendering-of-html-entities-issue

I believe the fix implemented above escapes all HTML entities from XML strings - including the actual XML structure itself.

So <someXml/> becomes &lt;someXml/&gt;

The problem that I am having is that I am using a template to serve XML as an API call, so the XML is returned to the caller escaped and therefore useless.

I have tried using:

#{verbatim} ${ anObject.someXml } #{/verbatim}

and:

${ anObject.someXml.raw() } 

to get the XML in raw format, but this does not work.

The part of the code that is causing this issue can be found in the groovy template__safeFaster method (line 400).

Does anyone know of a way I can get around this new feature?

Upvotes: 1

Views: 1337

Answers (2)

My Head Hurts
My Head Hurts

Reputation: 37675

As discussed in the question above, the the cause of the problem is in the __safeFaster method - or more specifically, the line:

if (template.name.endsWith(".xml"))
    return StringEscapeUtils.escapeXml(val.toString());

To work around this problem we are setting the response content-type to text/xml in the respective controller and changing the template extension to .txt.

Hopefully the developers at Play will fix this problem in their next release.

Upvotes: 2

Jaryn C.
Jaryn C.

Reputation: 65

If you're trying to render an XML String, you can use the renderXml method from play.mvc.Controller to display the formatted XML in your browser.

I tested this by adding this line to my conf/routes file:

# Render XML
GET    /renderXml    Application.renderXml(format:'xml')

Then creating this method in Application.java which reads an XML file and renders it (using Guava I/O):

    public static void renderXml() throws FileNotFoundException, IOException {      
    File xmlFile = new File("app/models", "xmlFile.xml");
    InputSupplier<InputStreamReader> inReader = Files.newReaderSupplier(xmlFile, Charsets.UTF_8);
    List<String> lines = CharStreams.readLines(inReader);

    String outputXml = "";

    for (String line : lines) {
        outputXml += line;
    }

    renderXml(outputXml);
}

Hope this helps!

Upvotes: 1

Related Questions