Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5

 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 acces 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" standalon="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->asXML();
  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 /joomla/utilities/simplexml.php (line 84)

Class JObject   (Subpackage Base)

Class JSimpleXML   (Subpackage Utilities)
Variable Summary
Variable object $document
Variable resource $_parser
Variable array $_stack
Variable string $_xml
Method Summary
Constructor JSimpleXML __construct ([ $options = null])
Method resource getParser ()
Method object JSimpleXMLElement importDom (string $node, [string $classname = null])
Method boolean loadFile (string $path, [string $classname = null])
Method object JSimpleXMLElement loadString (string $string, [string $classname = null])
Method void setParser (resource $parser)
Method void _characterData (resource $parser, string $data)
Method void _endElement (resource $parser, string $name)
Method object _getStackLocation ()
Method void _handleError (int $code, int $line, int $col)
Method void _parse ([$xml $data = ''])
Method void _startElement (resource $parser, string $name, [array $attrs = array()])
Variables
object $document = null (line 105)

Document element

resource $_parser = null (line 91)

The XML parser

array $_stack = array() (line 112)

Current object depth

string $_xml = '' (line 98)

The XML document

Methods
Constructor __construct (line 120)

Constructor.

  • access: protected
JSimpleXML __construct ([ $options = null])
  • $options

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

Get the parser

  • return: XML parser resource handle
  • access: public
resource getParser ()
importDom (line 197)

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.

object JSimpleXMLElement importDom (string $node, [string $classname = null])
  • string $node: DOM document
  • string $classname: currently ignored
loadFile (line 171)

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.

  • return: True if successful, false if file empty
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 155)

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 217)

Set the parser

  • access: public
void setParser (resource $parser)
  • resource $parser: XML parser resource handle
_characterData (line 327)

Handler function for the character data within a tag

  • access: protected
void _characterData (resource $parser, string $data)
  • resource $parser
  • string $data
_endElement (line 314)

Handler function for the end of a tag

  • access: protected
void _endElement (resource $parser, string $name)
  • resource $parser
  • string $name
_getStackLocation (line 261)

Gets the reference to the current direct parent

object _getStackLocation ()
_handleError (line 251)

Handles an XML parsing error

  • access: protected
void _handleError (int $code, int $line, int $col)
  • int $code: XML Error Code
  • int $line: Line on which the error happened
  • int $col: Column on which the error happened
_parse (line 228)

Start parsing an XML document

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

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

Handler function for the start of a tag

  • access: protected
void _startElement (resource $parser, string $name, [array $attrs = array()])
  • resource $parser
  • string $name
  • array $attrs

Inherited Methods

Inherited From JObject

 JObject::JObject()
 JObject::__construct()
 JObject::get()
 JObject::getPublicProperties()
 JObject::set()
 JObject::toString()

Documentation generated on Mon, 05 Mar 2007 21:25:20 +0000 by phpDocumentor 1.3.1