The XML formatter is serializing array paramters with the attribute: is_array="true"
and child elements of that parameter are being wrapped in a <item> </item> element.
The resulting format of an request, for example, for a parameter named "array_parameter" is:
<array_parameter is_array="true">
<item>
<contents>example contents 1</contents>
</item>
<item>
<contents>example contents 2</contents>
</item>
<item>
<contents>example contents 3</contents>
</item>
</array_parameter>
I understand that this may be the correct format when integrating with the Drupal Services module, however it is not the correct format for many other types of web services.
This functionality should either be removed, or there should be an option to enable it. By default, the request format should look like this:
<array_parameter>
<contents>example contents 1</contents>
<contents>example contents 2</contents>
<contents>example contents 3</contents>
</array_parameter>
This problem derives from the xml_recurse function from within HTTP Client.
This is the code from /http_client/includes/HttpClientXMLFormatter.inc Lines 116 - 144 that contains the xml_recurse function:
protected function xml_recurse(&$doc, &$parent, $data) {
if (is_object($data)) {
$data = get_object_vars($data);
}
if (is_array($data)) {
$assoc = FALSE || empty($data);
foreach ($data as $key => $value) {
if (is_numeric($key)) {
$key = 'item';
}
else {
$assoc = TRUE;
$key = $this->sanitizeNodeName($key);
}
$element = $doc->createElement($key);
$parent->appendChild($element);
$this->xml_recurse($doc, $element, $value);
}
if (!$assoc) {
$parent->setAttribute('is_array', 'true');
}
}
else if ($data !== NULL) {
$parent->appendChild($doc->createTextNode($data));
}
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | http_client-2120755-XMLFormatter-3.patch | 2.1 KB | freddura |
Comments
Comment #1
freddura commentedModified the code referenced above from /http_client/includes/HttpClientXMLFormatter.inc Lines 116 - 144, effectively solving the issue. Also added some comments to clarify what is happening:
With this new code, the XML formatter is no longer serializing array elements with the attribute:
is_array="true"nor wrapping its child elements in an<item> </item>element. This resulting format of the request now looks as desired:Comment #2
freddura commentedMade some improvements to the code to support the use of attributes, see below:
Comment #3
freddura commentedHere's the patch:
Comment #4
freddura commentedComment #4.0
freddura commentedtypo