Location: PHPKode > scripts > XParser > classes/xparser_php530.class.php
<?php
/**
 * Parsing an XML document in PHP is always a hard work.
 * This class allow us to parse an XML document in an easy way.
 * PHP version >= 5.3.0
 * @author Gonzalo Chumillas <hide@address.com>
 */
class XParser implements Iterator, ArrayAccess {
	private $items;

	/**
	 * Creates a new instance of XParser.
	 * @param string|DOMNodeList|DOMNode $obj
	 */
	public function XParser($obj) {
		if (is_string($obj)) {
			$this->loadFile($obj);
		} else
		if ($obj instanceof DOMNodeList) {
			$this->items = array();
			foreach ($obj as $item) {
				array_push($this->items, $item);
			}
		} else
		if ($obj instanceof DOMNode) {
			$this->items = array($obj);
		}
	}

	/**
	 * Whether or not an offset exists.
	 * @param int $offset
	 * @return boolean
	 */
	public function offsetExists($offset) {
		return array_key_exists($offset, $this->items);
	}

	/**
	 * Returns the value at specified offset.
	 * @param int $offset
	 * @return DOMNode
	 */
	public function offsetGet($offset) {
		return $this->items[$offset];
	}

	/**
	 * Assigns a value to the specified offset.
	 * @param int $offset
	 * @param DOMNode $value
	 */
	public function offsetSet($offset, $value) {
		$this->items[$offset] = $value;
	}

	/**
	 * Unsets an offset.
	 * @param int $offset
	 */
	public function offsetUnset($offset) {
		unset($this->items[$offset]);
	}

	/**
	 *
	 * @param string $name
	 * @return string
	 */
	public function __get($name) {
		return current($this->items)->getAttribute($name);
	}

	/**
	 *
	 * @param string $xpath
	 * @return XParser
	 */
	public function __invoke($xpath) {
		return $this->search($xpath);
	}

	/**
	 * Loads an XML document from a file
	 * @param String $filename
	 */
	public function loadFile($filename) {
		$this->items = array();
		$doc = new DOMDocument("1.0", "iso-8859-1");
		$doc->preserveWhiteSpace = false;
		$doc->load($filename);
		$this->items = array($doc->documentElement);
	}

	/**
	 * Loads an XML document from a string
	 * @param Strig $str
	 */
	public function loadString($str) {
		$this->items = array();
		$doc = new DOMDocument("1.0", "iso-8859-1");
		$doc->preserveWhiteSpace = false;
		$doc->loadXML($str);
		$this->items = array($doc->documentElement);
	}

	/**
	 * Returns a DOM object specified by index.
	 * @param int $i
	 * @return DOMNode
	 */
	public function get($i) {
		return $this->items[$i];
	}

	/**
	 * Returns the number of the nodes.
	 * @return int
	 */
	public function length() {
		return sizeof($this->items);
	}

	/**
	 * Returns the current node.
	 * @return XParser
	 */
	public function current() {
		$ret = FALSE;
		$current = current($this->items);
		if ($current !== FALSE) {
			$ret = new XParser($current);
		}
		return $ret;
	}

	/**
	 * Moves forward to next node.
	 * @return XParser
	 */
	public function next() {
		$ret = FALSE;
		$next = next($this->items);
		if ($next !== FALSE) {
			$ret = new XParser($next);
		}
		return $ret;
	}

	/**
	 * Returns the internal pointer.
	 * @return int
	 */
	public function key() {
		return key($this->items);
	}

	/**
	 * Rewinds the internal pointer.
	 */
	public function rewind() {
		reset($this->items);
	}

	/**
	 * Checks if current position is valid.
	 * @return bool
	 */
	public function valid() {
		return (key($this->items) !== NULL);
	}

	/**
	 * Evaluates the given XPath expression and returns an XParser containing all nodes matching it.
	 * @param string $xquery
	 * @return XParser
	 */
	public function search($xquery) {
		$current = current($this->items);
		$xpath = new DOMXPath($current->ownerDocument);
		return new XParser($xpath->query($xquery, $current));
	}

	/**
	 * Returns the more accurate name for the current node type.
	 * @return string
	 */
	public function name() {
		return current($this->items)->nodeName;
	}

	/**
	 * Returns value of attribute.
	 * @param string $name
	 * @return string
	 */
	public function attr($name) {
		return current($this->items)->getAttribute($name);
	}

	/**
	 * The value of this node, depending on its type.
	 * @return String
	 */
	public function __toString() {
		$ret = "";
		$len = sizeof($this->items);
		for ($i = 0; $i < $len; $i++) {
			$ret .= $this->items[$i]->nodeValue;
		}
		return $ret;
	}
}
?>
Return current item: XParser