Array to XML in Symfony

Simple example how to convert Array to XML:

public function toXML() {
    $array = [
      'fields' => [
        'field' => [
          '@name' => 'name',
          '@test' => 'test',
          '#' => 12345
        ]
      ]
    ];

    $xmlEncoder = new XmlEncoder();

    $response = new Response();
    $response->setContent($xmlEncoder->encode($array, 'xml'));
    $response->headers->set('Content-Type', 'xml');
    return $response;
  }

Leave a Reply

Your email address will not be published.