You are here

public function XmlEncoder::encode in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/serializer/Encoder/XmlEncoder.php \Symfony\Component\Serializer\Encoder\XmlEncoder::encode()
  2. 8 core/modules/serialization/src/Encoder/XmlEncoder.php \Drupal\serialization\Encoder\XmlEncoder::encode()
Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Encoder/XmlEncoder.php \Symfony\Component\Serializer\Encoder\XmlEncoder::encode()

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

Throws

UnexpectedValueException

Overrides EncoderInterface::encode

File

vendor/symfony/serializer/Encoder/XmlEncoder.php, line 47

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 = $this
    ->createDomDocument($context);
  $this->format = $format;
  $this->context = $context;
  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();
}