The XML Formatter always adds a tag called 'result' to any XML requests made. This is often undesirable in client software and I presume it's a throwback to the XML Formatter's original purpose as a response formatter. This feature should either be a) removed or b) made configurable either by a specific property on the formatter, or smarter detection of an appropriate root element from the input data.

A patch will be forthcoming, but I thought I'd kick off a debate about the best way to do it first.

Comments

Rob Knight’s picture

Status: Active » Needs review
StatusFileSize
new1.09 KB

I've attached a patch which does the following:

If $data is an array with a single element, and that element has a non-numeric key, treat the key as the tag to be used for the root XML node, and the element will be used as the data. In other words:

<?php
$data = array('foo' => array('bar' => 123));
?>

should yield the XML:

<foo>
  <bar>123</bar>
</foo>

This solves the problem whereby a element would always be used as the root node of the XML document, even when this was not desired.

I'm posting this as a patch, but I think it's an open question as to whether this should be implemented as an alternative formatter class. I'm happy for the maintainer(s) decide what they want :-)

Hugo Wetterberg’s picture

Hi Rob, sorry about the lack of action in the issue queue. Here's an expanded patch that makes the XmlFormatter configurable, with options to change the default root element name and/or use an adaptive root name. Both are opt in features configurable through the constructor with backwards compatible defaults.

It would be great if you could give it a spin, nobody here uses XML, so any feedback is appreciated.

Hugo Wetterberg’s picture

Assigned: Unassigned » Hugo Wetterberg
Hugo Wetterberg’s picture

Status: Needs review » Closed (fixed)

Fixed in 2.3