You are here

function forena_xmlpp in Forena Reports 7

Same name and namespace in other branches
  1. 8 forena_ui/forena.admin.inc \forena_xmlpp()
  2. 6.2 forena.admin.inc \forena_xmlpp()
  3. 6 forena.admin.inc \forena_xmlpp()
  4. 7.5 forena.admin.inc \forena_xmlpp()
  5. 7.2 forena.admin.inc \forena_xmlpp()
  6. 7.3 forena.admin.inc \forena_xmlpp()
  7. 7.4 forena.admin.inc \forena_xmlpp()

Prettifies an XML string into a human-readable and indented work of art

Parameters

string $xml The XML as a string: @param boolean $html_output True if the output should be escaped (for use in HTML)

File

./forena.admin.inc, line 1260

Code

function forena_xmlpp($xml, $html_output = FALSE) {
  $xml_obj = new SimpleXMLElement($xml);
  $level = 4;
  $indent = 0;

  // current indentation level
  $pretty = array();

  // get an array containing each XML element
  $xml = explode("\n", preg_replace('/>\\s*</', ">\n<", $xml_obj
    ->asXML()));

  // shift off opening XML tag if present
  if (count($xml) && preg_match('/^<\\?\\s*xml/', $xml[0])) {
    $pretty[] = array_shift($xml);
  }
  foreach ($xml as $el) {
    if (preg_match('/^<([\\w])+[^>\\/]*>$/U', $el)) {

      // opening tag, increase indent
      $pretty[] = str_repeat(' ', $indent) . $el;
      $indent += $level;
    }
    else {
      if (preg_match('/^<\\/.+>$/', $el)) {
        $indent -= $level;

        // closing tag, decrease indent
      }
      if ($indent < 0) {
        $indent += $level;
      }
      $pretty[] = str_repeat(' ', $indent) . $el;
    }
  }
  $xml = implode("\n", $pretty);
  return $html_output ? htmlentities($xml) : $xml;
}