You are here

protected function FormatterHalXml::arrayToXML in RESTful 7.2

Converts the input array into an XML formatted string.

Parameters

array $data: The input array.

\SimpleXMLElement $xml: The object that will perform the conversion.

Return value

\SimpleXMLElement

File

modules/restful_example/src/Plugin/formatter/FormatterHalXml.php, line 54
Contains \Drupal\restful\Plugin\formatter\FormatterHalJson.

Class

FormatterHalXml
Class FormatterHalXml @package Drupal\restful\Plugin\formatter

Namespace

Drupal\restful_example\Plugin\formatter

Code

protected function arrayToXML(array $data, \SimpleXMLElement $xml) {
  foreach ($data as $key => $value) {
    if (is_array($value)) {
      if (!is_numeric($key)) {
        $subnode = $xml
          ->addChild("{$key}");
        $this
          ->arrayToXML($value, $subnode);
      }
      else {
        $subnode = $xml
          ->addChild("item{$key}");
        $this
          ->arrayToXML($value, $subnode);
      }
    }
    else {
      $xml
        ->addChild("{$key}", htmlspecialchars("{$value}"));
    }
  }
  return $xml;
}