function format_xml_elements

Formats XML elements.

Parameters

$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

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

Related topics

7 calls to format_xml_elements()
FileFieldRSSContentTest::testFileFieldRSSContent in drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldRSSContentTest.php
Tests RSS enclosure formatter display for RSS feeds.
format_rss_channel in drupal/core/includes/common.inc
Formats an RSS channel.
format_rss_item in drupal/core/includes/common.inc
Formats a single RSS item.
NodeRSSContentTest::testNodeRSSContent in drupal/core/modules/node/lib/Drupal/node/Tests/NodeRSSContentTest.php
Ensures that a new node includes the custom data when added to an RSS feed.
RssTest::testTaxonomyRss in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php
Tests that terms added to nodes are displayed in core RSS feed.

... See full list

File

drupal/core/includes/common.inc, line 1617
Common functions that many Drupal modules will need to reference.

Code

function format_xml_elements($array) {
  $output = '';
  foreach ($array as $key => $value) {
    if (is_numeric($key)) {
      if ($value['key']) {
        $output .= ' <' . $value['key'];
        if (isset($value['attributes']) && is_array($value['attributes'])) {
          $output .= new Attribute($value['attributes']);
        }
        if (isset($value['value']) && $value['value'] != '') {
          $output .= '>' . (is_array($value['value']) ? format_xml_elements($value['value']) : check_plain($value['value'])) . '</' . $value['key'] . ">\n";
        }
        else {
          $output .= " />\n";
        }
      }
    }
    else {
      $output .= ' <' . $key . '>' . (is_array($value) ? format_xml_elements($value) : check_plain($value)) . "</{$key}>\n";
    }
  }
  return $output;
}