Class JSimpleXML

Description

SimpleXML implementation.

The XML Parser extension (expat) is required to use JSimpleXML.

The class provides a pure PHP4 implementation of the PHP5 interface SimpleXML. As with PHP5's SimpleXML it is what it says: simple. Nevertheless, it is an easy way to deal with XML data, especially for read only access.

Because it's not possible to use the PHP5 ArrayIterator interface with PHP4 there are some differences between this implementation and that of PHP5:

The access to the root node has to be explicit in JSimpleXML, not implicit as with PHP5. Write $xml->document->node instead of $xml->node You cannot access CDATA using array syntax. Use the method data() instead You cannot access attributes directly with array syntax. Use attributes() to read them. Comments are ignored. Last and least, this is not as fast as PHP5 SimpleXML--it is pure PHP4.

Example:

  1.  :simple.xml:
  2.  <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  3.  <document>
  4.    <node>
  5.     <child gender="m">Tom Foo</child>
  6.     <child gender="f">Tamara Bar</child>
  7.    <node>
  8.  </document>
  9.  
  10.  ---
  11.  
  12.  // read and write a document
  13.  $xml = new JSimpleXML;
  14.  $xml->loadFile('simple.xml');
  15.  print $xml->document->toString();
  16.  
  17.  // access a given node's CDATA
  18.  print $xml->root->node->child[0]->data(); // Tom Foo
  19.  
  20.  // access attributes
  21.  $attr = $xml->root->node->child[1]->attributes();
  22.  print $attr['gender']; // f
  23.  
  24.  // access children
  25.  foreach($xml->root->node->children() as $child) {
  26.    print $child->data();
  27.  }

Note: JSimpleXML cannot be used to access sophisticated XML doctypes using datatype ANY (e.g. XHTML). With a DOM implementation you can handle this.

Located in /libraries/joomla/utilities/simplexml.php (line 76)

JObject
   |
   --JSimpleXML
Variable Summary
object $document
resource $_parser
array $_stack
Method Summary
JSimpleXML __construct ([array $options = null])
resource getParser ()
mixed importDOM (string $node, [string $classname = null])
boolean loadFile (string $path, [string $classname = null])
object JSimpleXMLElement loadString (string $string, [string $classname = null])
void setParser (resource $parser)
void _characterData (resource $parser, string $data)
void _endElement (resource $parser, string $name)
object _getStackLocation ()
void _handleError (integer $code, integer $line, integer $col)
void _parse ([string $data = ''])
void _startElement (resource $parser, string $name, [array $attrs = array()])
Variables
object $document = null (line 92)

Document element

  • since: 11.1
  • access: public
resource $_parser = null (line 84)

The XML parser

  • since: 11.1
  • access: private
array $_stack = array() (line 100)

Current object depth

  • since: 11.1
  • access: private

Inherited Variables

Inherited from JObject

JObject::$_errors
Methods
Constructor __construct (line 111)

Constructor.

JSimpleXML __construct ([array $options = null])
  • array $options: Options

Redefinition of:
JObject::__construct()
Class constructor, overridden in descendant classes.
getParser (line 243)

Get the parser

resource getParser ()
importDOM (line 226)

Get a JSimpleXMLElement object from a DOM node.

This function takes a node of a DOM document and makes it into a JSimpleXML node. This new object can then be used as a native JSimpleXML element. If any errors occur, it returns FALSE.

mixed importDOM (string $node, [string $classname = null])
  • string $node: DOM document
  • string $classname: currently ignored
loadFile (line 184)

Interprets an XML file into an object

This function will convert the well-formed XML document in the file specified by filename to an object of class JSimpleXMLElement. If any errors occur during file access or interpretation, the function returns FALSE.

boolean loadFile (string $path, [string $classname = null])
  • string $path: Path to XML file containing a well-formed XML document
  • string $classname: currently ignored
loadString (line 158)

Interprets a string of XML into an object

This function will take the well-formed XML string data and return an object of class JSimpleXMLElement with properties containing the data held within the XML document. If any errors occur, it returns FALSE.

object JSimpleXMLElement loadString (string $string, [string $classname = null])
  • string $string: Well-formed XML string data
  • string $classname: currently ignored
setParser (line 262)

Set the parser

void setParser (resource $parser)
  • resource $parser: XML parser resource handle.
_characterData (line 421)

Handler function for the character data within a tag

  • deprecated: 12.1
  • since: 11.1
  • access: protected
void _characterData (resource $parser, string $data)
  • resource $parser: The XML parser.
  • string $data: The CDATA for the element.
_endElement (line 401)

Handler function for the end of a tag

  • deprecated: 12.1
  • since: 11.1
  • access: protected
void _endElement (resource $parser, string $name)
  • resource $parser: The XML parser.
  • string $name: The name of the element.
_getStackLocation (line 333)

Gets the reference to the current direct parent

  • deprecated: 12.1
  • since: 11.1
  • access: protected
object _getStackLocation ()
_handleError (line 315)

Handles an XML parsing error

  • deprecated: 12.1 Use PHP Exception
  • deprecated: 12.1
  • since: 11.1
  • access: protected
void _handleError (integer $code, integer $line, integer $col)
  • integer $code: XML Error Code.
  • integer $line: Line on which the error happened.
  • integer $col: Column on which the error happened.
_parse (line 283)

Start parsing an XML document

Parses an XML document. The handlers for the configured events are called as many times as necessary.

void _parse ([string $data = ''])
  • string $data: data to parse
_startElement (line 360)

Handler function for the start of a tag

  • deprecated: 12.1
  • since: 11.1
  • access: protected
void _startElement (resource $parser, string $name, [array $attrs = array()])
  • resource $parser: The XML parser.
  • string $name: The name of the element.
  • array $attrs: A key-value array (optional) of the attributes for the element.

Inherited Methods

Inherited From JObject

JObject::__construct()
JObject::def()
JObject::get()
JObject::getError()
JObject::getErrors()
JObject::getProperties()
JObject::set()
JObject::setError()
JObject::setProperties()
JObject::toString()
JObject::__toString()

Documentation generated on Mon, 25 Jun 2012 13:59:36 -0500 by phpDocumentor 1.4.4