public function XmlEncoder::encode

Same name in this branch

Encodes data into the given format

Parameters

mixed $data Data to encode:

string $format Format name:

array $context options that normalizers/encoders have access to.:

Return value

scalar

Overrides EncoderInterface::encode

File

drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Encoder/XmlEncoder.php, line 42

Class

XmlEncoder
Encodes XML data

Namespace

Symfony\Component\Serializer\Encoder

Code

public function encode($data, $format, array $context = array()) {
  if ($data instanceof \DOMDocument) {
    return $data
      ->saveXML();
  }
  $xmlRootNodeName = $this
    ->resolveXmlRootName($context);
  $this->dom = new \DOMDocument();
  $this->format = $format;
  if (null !== $data && !is_scalar($data)) {
    $root = $this->dom
      ->createElement($xmlRootNodeName);
    $this->dom
      ->appendChild($root);
    $this
      ->buildXml($root, $data, $xmlRootNodeName);
  }
  else {
    $this
      ->appendNode($this->dom, $data, $xmlRootNodeName);
  }
  return $this->dom
    ->saveXML();
}