You are here

class JSimpleXMLElement in Ubercart 5

SimpleXML Element

This object stores all of the direct children of itself in the $children array. They are also stored by type as arrays. So, if, for example, this tag had 2 <font> tags as children, there would be a class member called $font created as an array. $font[0] would be the first font tag, and $font[1] would be the second.

To loop through all of the direct children of this object, the $children member should be used.

To loop through all of the direct children of a specific tag for this object, it is probably easier to use the arrays of the specific tag names, as explained above.

@author Johan Janssens <johan.janssens@joomla.org> @package Joomla.Framework @subpackage Utilities @since 1.5

Hierarchy

Expanded class hierarchy of JSimpleXMLElement

File

uc_store/includes/simplexml.php, line 369

View source
class JSimpleXMLElement {

  /**
   * Array with the attributes of this XML element
   *
   * @var array
   */
  var $_attributes = array();

  /**
   * The name of the element
   *
   * @var string
   */
  var $_name = '';

  /**
   * The data the element contains
   *
   * @var string
   */
  var $_data = '';

  /**
   * Array of references to the objects of all direct children of this XML object
   *
   * @var array
   */
  var $_children = array();

  /**
   * The level of this XML element
   *
   * @var int
   */
  var $_level = 0;

  /**
   * A hack to support __construct() on PHP 4
   *
   * Hint: descendant classes have no PHP4 class_name() constructors,
   * so this constructor gets called first and calls the top-layer __construct()
   * which (if present) should call parent::__construct()
   *
   * @access	public
   * @return	Object
   * @since	1.5
   */
  function JSimpleXMLElement() {
    $args = func_get_args();
    call_user_func_array(array(
      &$this,
      '__construct',
    ), $args);
  }

  /**
   * Constructor, sets up all the default values
   *
   * @param string $name
   * @param array $attrs
   * @param int $parents
   * @return JSimpleXMLElement
   */
  function __construct($name, $attrs = array(), $level = 0) {

    //Make the keys of the attr array lower case, and store the value
    $this->_attributes = array_change_key_case($attrs, CASE_LOWER);

    //Make the name lower case and store the value
    $this->_name = strtolower($name);

    //Set the level
    $this->_level = $level;
  }

  /**
   * Get the name of the element
   *
   * @access public
   * @return string
   */
  function name() {
    return $this->_name;
  }

  /**
   * Get the an attribute of the element
   *
   * @param string $attribute 	The name of the attribute
   *
   * @access public
   * @return mixed If an attribute is given will return the attribute if it exist.
   * 				 If no attribute is given will return the complete attributes array
   */
  function attributes($attribute = null) {
    if (!isset($attribute)) {
      return $this->_attributes;
    }
    return isset($this->_attributes[$attribute]) ? $this->_attributes[$attribute] : null;
  }

  /**
   * Get the data of the element
   *
   * @access public
   * @return string
   */
  function data() {
    return $this->_data;
  }

  /**
   * Set the data of the element
   *
   * @access public
   * @param	string $data
   * @return string
   */
  function setData($data) {
    $this->_data = $data;
  }

  /**
   * Get the children of the element
   *
   * @access public
   * @return array
   */
  function children() {
    return $this->_children;
  }

  /**
   * Get the level of the element
   *
   * @access public
   * @return int
   */
  function level() {
    return $this->_level;
  }

  /**
   * Adds an attribute to the element
   *
   * @param string $name
   * @param array  $attrs
   */
  function addAttribute($name, $value) {

    //add the attribute to the element, override if it already exists
    $this->_attributes[$name] = $value;
  }

  /**
   * Removes an attribute from the element
   *
   * @param string $name
   */
  function removeAttribute($name) {
    unset($this->_attributes[$name]);
  }

  /**
   * Adds a direct child to the element
   *
   * @param string $name
   * @param array  $attrs
   * @param int 	 $level
   */
  function addChild($name, $attrs, $level) {

    //If there is no array already set for the tag name being added,

    //create an empty array for it
    if (!isset($this->{$name})) {
      $this->{$name} = array();
    }

    //Create the child object itself
    $child = new JSimpleXMLElement($name, $attrs, $level);

    //Add the reference of it to the end of an array member named for the elements name
    $this->{$name}[] =& $child;

    //Add the reference to the children array member
    $this->_children[] =& $child;
  }
  function removeChild(&$child) {
    $name = $child
      ->name();
    for ($i = 0, $n = count($this->_children); $i < $n; $i++) {
      if ($this->_children[$i] == $child) {
        unset($this->_children[$i]);
      }
    }
    for ($i = 0, $n = count($this->{$name}); $i < $n; $i++) {
      if ($this->{$name}[$i] == $child) {
        unset($this->{$name}[$i]);
      }
    }
    $this->_children = array_values($this->_children);
    $this->{$name} = array_values($this->{$name});
    unset($child);
  }

  /**
   * Get an element in the document by / separated path
   *
   * @param	string	$path	The / separated path to the element
   * @return	object	JSimpleXMLElement
   */
  function &getElementByPath($path) {
    $tmp =& $this;
    $false = false;
    $parts = explode('/', trim($path, '/'));
    foreach ($parts as $node) {
      $found = false;
      foreach ($tmp->_children as $child) {
        if ($child->_name == $node) {
          $tmp =& $child;
          $found = true;
          break;
        }
      }
      if (!$found) {
        break;
      }
    }
    if ($found) {
      $ref =& $tmp;
    }
    else {
      $ref =& $false;
    }
    return $ref;
  }
  function map($callback, $args = array()) {
    $callback($this, $args);

    // Map to all children
    if ($n = count($this->_children)) {
      for ($i = 0; $i < $n; $i++) {
        $this->_children[$i]
          ->map($callback, $args);
      }
    }
  }

  /**
   * Return a well-formed XML string based on SimpleXML element
   *
   * @return string
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JSimpleXMLElement::$_attributes property Array with the attributes of this XML element
JSimpleXMLElement::$_children property Array of references to the objects of all direct children of this XML object
JSimpleXMLElement::$_data property The data the element contains
JSimpleXMLElement::$_level property The level of this XML element
JSimpleXMLElement::$_name property The name of the element
JSimpleXMLElement::addAttribute function Adds an attribute to the element
JSimpleXMLElement::addChild function Adds a direct child to the element
JSimpleXMLElement::asXML function Return a well-formed XML string based on SimpleXML element
JSimpleXMLElement::attributes function Get the an attribute of the element
JSimpleXMLElement::children function Get the children of the element
JSimpleXMLElement::data function Get the data of the element
JSimpleXMLElement::getElementByPath function Get an element in the document by / separated path
JSimpleXMLElement::JSimpleXMLElement function A hack to support __construct() on PHP 4
JSimpleXMLElement::level function Get the level of the element
JSimpleXMLElement::map function
JSimpleXMLElement::name function Get the name of the element
JSimpleXMLElement::removeAttribute function Removes an attribute from the element
JSimpleXMLElement::removeChild function
JSimpleXMLElement::setData function Set the data of the element
JSimpleXMLElement::__construct function Constructor, sets up all the default values