Problem/Motivation

I am currently working on a resource for my application. As different formats (JSON/XML) should be presented in identical structures, i am now struggling with the given root element "<result>" of the render_xml-method in rest_server.views.inc.
The specification needs to create a response like this

<?xml version="1.0" encoding="utf-8"?>
  <service_definition>
   <service_code>DMV66</service_code> 
   <attributes>
    etc ...

Proposed resolution

Is there a way to override this function? Or maybe something like this would be a good solution for always applying the first key of the data-array as root-element:

  private function render_xml($data) {
    $doc = new DOMDocument('1.0', 'utf-8');
    // $root = $doc->createElement('result');
    $root = $doc->createElement(key($data));
    $doc->appendChild($root);
    // shifting all children to top-level
    $data = $data[key($data)];
    // deleting first key
    unset($data[key($data)]);
    $this->xml_recurse($doc, $root, $data);

    return $doc->saveXML();
  }

Any other ideas very much appreciated ...

Comments

markaspot’s picture

Status: Active » Needs review
ygerasimov’s picture

Status: Needs review » Fixed

I wouldn't like to change this as this is big API change and current clients will break.

If your specifications require these changes, I would suggest to alter formatters and have your own custom function to prepare output for XML.

Please reopen this ticket if you have any further questions.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

markaspot’s picture

thanks for the reply .. i will try to implement hook_services_request_postprocess_alter

markaspot’s picture

Issue summary: View changes

added some code to render_xml ...

jeroen.b’s picture

I also had this problem. My XML had to match some XSD provided by another company.
The way I did this was with a custom formatter.

function HOOK_rest_server_response_formatters_alter(&$formatters) {
  $formatters['custom_xml'] = $formatters['xml'];
  $formatters['custom_xml']['formatter class'] = 'CustomServicesXMLFormatter';
}

class CustomServicesXMLFormatter implements ServicesFormatterInterface {
  public function render($data) {
    $doc = new DOMDocument('1.0', 'utf-8');
    $root = $doc->createElement('response');
    $root->setAttribute('xmlns', 'mycustomns');
    $doc->appendChild($root);

    $this->xml_recursive($doc, $root, $data);

    return $doc->saveXML();
  }

  private function xml_recursive(&$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 = preg_replace('/[^A-Za-z0-9_]/', '_', $key);
          $key = preg_replace('/^([0-9]+)/', '_$1', $key);
        }
        $element = $doc->createElement($key);
        $parent->appendChild($element);
        $this->xml_recursive($doc, $element, $value);
      }

      if (!$assoc) {
        $parent->setAttribute('is_array', 'true');
      }
    }
    elseif ($data !== NULL) {
      $parent->appendChild($doc->createTextNode($data));
    }
  }
}