You are here

function node_export_xml_encode in Node export 7.3

Same name and namespace in other branches
  1. 6.3 modules/node_export_xml/node_export_xml.module \node_export_xml_encode()

Build XML string recursively.

1 call to node_export_xml_encode()
node_export_xml_export in formats/xml.inc
Export callback.

File

formats/xml.inc, line 46
The Node export XML format handler.

Code

function node_export_xml_encode($var, $iteration = 0) {
  $xml_code = "";
  $tab = '';
  for ($i = 0; $i <= $iteration; $i++) {
    $tab = $tab . "  ";
  }
  $iteration++;
  foreach ($var as $key => $value) {
    $attributes = array();
    if (is_bool($value)) {
      $attributes['type'] = 'boolean';
    }
    elseif (is_null($value)) {
      $attributes['type'] = 'NULL';
    }
    elseif (is_object($value)) {
      if ($iteration == 1 && isset($value->nid) && isset($value->type)) {

        // Assume first-level object with a nid and type is a stdClass node.
        $key = "node";
      }
      else {
        $attributes['class'] = get_class($value);
      }
      $value = (array) $value;
    }
    if (is_array($value) && array_values($value) === $value) {
      $attributes['_numeric_keys'] = "1";
    }
    $attr_string = "";
    foreach ($attributes as $attr_name => $attr_value) {
      $attr_string .= ' ' . $attr_name . '="' . $attr_value . '"';
    }
    if (is_numeric($key)) {
      $key = "n" . $key;
    }
    $xml_code .= $tab . "<" . $key . $attr_string . ">";
    if (is_array($value)) {
      if (!empty($value)) {
        $xml_code .= "\n";
        $xml_code .= node_export_xml_encode($value, $iteration);
        if (!is_numeric($key)) {
          $xml_code .= $tab;
        }
      }
    }
    elseif (is_numeric($value)) {
      $xml_code .= $value;
    }
    elseif (is_bool($value)) {
      $xml_code .= $value ? 'TRUE' : 'FALSE';
    }
    elseif (is_string($value)) {
      $xml_code .= htmlspecialchars($value);
    }
    $xml_code .= "</" . $key . ">\n";
  }
  return $xml_code;
}