You are here

function array_to_xml in TableField 7.3

Convert array to XML. See http://stackoverflow.com/a/5965940/523688.

1 call to array_to_xml()
tablefield_field_formatter_view in ./tablefield.module
Implements hook_field_formatter_view().

File

./tablefield.module, line 2559
Provides a set of fields that can be used to store tabular data with a node.

Code

function array_to_xml($data, &$xml_data) {
  foreach ($data as $key => $value) {
    if (is_numeric($key)) {

      // Dealing with <0/>..<n/> issues.
      $key = 'item_' . $key;
    }

    // Within keys (XML element tags) only accept A-Z (case insensitive), 0-9,
    // a dash (-) and an underscore (_). Replace all others with an underscore.
    // Avoid multiple consecutive underscores.
    $key = preg_replace('/_+/', '_', preg_replace('/[^A-Za-z0-9_\\-]/', '_', $key));

    // If the <3element> starts with a number, prefix it with an underscore
    // <_3element> to get valid XML.
    $key = is_numeric(substr($key, 0, 1)) ? substr_replace($key, '_', 0, 0) : $key;
    if (is_array($value)) {
      $subnode = $xml_data
        ->addChild($key);
      array_to_xml($value, $subnode);
    }
    else {
      $xml_data
        ->addChild("{$key}", htmlspecialchars("{$value}"));
    }
  }
}