1 common.inc format_xml_elements($array, $indentation_level = 0)

Recursively formats nested XML array elements as a string.

@since 1.12.7 Added $indentation_level parameter.

Parameters

array $array: An array where each item represents an element and is either a:

  • (key => value) pair (<key>value</key>)
  • Associative array with fields:
    • 'key': element name
    • 'value': element contents
    • 'attributes': associative array of element attributes
    • 'encoded': TRUE if 'value' is already encoded

In both cases, 'value' can be a simple string, or it can be another array with the same format as $array itself for nesting.

If 'encoded' is TRUE it is up to the caller to ensure that 'value' is either entity-encoded or CDATA-escaped. Using this option is not recommended when working with untrusted user input, since failing to escape the data correctly has security implications.

int $indentation_level: Internal-use only indentation level. This function is called recursively and this parameter is used to increase indentation in each nesting.

Return value

string: A string of XML representing the elements passed in.

Related topics

File

core/includes/common.inc, line 2171
Common functions that many Backdrop modules will need to reference.

Code

function format_xml_elements($array, $indentation_level = 0) {
  $output = '';

  // Indent two spaces per level.
  $indentation = str_repeat(' ', $indentation_level * 2);

  foreach ($array as $key => $value) {
    if (is_numeric($key)) {
      if ($value['key']) {
        $output .= $indentation . '<' . $value['key'];
        if (isset($value['attributes']) && is_array($value['attributes'])) {
          $output .= backdrop_attributes($value['attributes']);
        }

        if (isset($value['value']) && $value['value'] != '') {
          $output .= '>';
          if (is_array($value['value'])) {
            $output .= "\n" . format_xml_elements($value['value'], $indentation_level + 1);
            $output .= $indentation . '</' . $value['key'] . ">\n";
          }
          else {
            if (!empty($value['encoded'])) {
              $output .= $value['value'];
            }
            else {
              $output .= check_plain($value['value']);
            }
            $output .= '</' . $value['key'] . ">\n";
          }
        }
        else {
          $output .= " />\n";
        }
      }
    }
    else {
      $output .= $indentation . '<' . $key . '>';
      if (is_array($value)) {
        $output .= "\n" . format_xml_elements($value, $indentation_level + 1);
        $output .= $indentation . "</$key>\n";
      }
      else {
        $output .= check_plain($value) . "</$key>\n";
      }
    }
  }
  return $output;
}