You are here

function JSimpleXMLElement::asXML in Ubercart 5

Return a well-formed XML string based on SimpleXML element

Return value

string

File

uc_store/includes/simplexml.php, line 631

Class

JSimpleXMLElement
SimpleXML Element

Code

function asXML($whitespace = true) {

  //Start a new line, indent by the number indicated in $this->level, add a <, and add the name of the tag
  if ($whitespace) {
    $out = "\n" . str_repeat("\t", $this->_level) . '<' . $this->_name;
  }
  else {
    $out = '<' . $this->_name;
  }

  //For each attribute, add attr="value"
  foreach ($this->_attributes as $attr => $value) {
    $out .= ' ' . $attr . '="' . $value . '"';
  }

  //If there are no children and it contains no data, end it off with a />
  if (empty($this->_children) && empty($this->_data)) {
    $out .= " />";
  }
  else {

    //If there are children
    if (!empty($this->_children)) {

      //Close off the start tag
      $out .= '>';

      //For each child, call the asXML function (this will ensure that all children are added recursively)
      foreach ($this->_children as $child) {
        $out .= $child
          ->asXML($whitespace);
      }

      //Add the newline and indentation to go along with the close tag
      if ($whitespace) {
        $out .= "\n" . str_repeat("\t", $this->_level);
      }
    }
    elseif (!empty($this->_data)) {
      $out .= '>' . $this->_data;
    }

    //Add the end tag
    $out .= '</' . $this->_name . '>';
  }

  //Return the final output
  return $out;
}