Support Joomla!

references:joomla.framework:utilities:jsimplexml

JSimpleXML

API Package Subpackage Class Reference Last reviewed Doc status
API Home Package Joomla.Framework Subpackage Utilities Class JSimpleXML Reference JSimpleXML Never Work in Progress

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.

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

Methods

Method Description
__construct Constructor.
getParser Get the parser
importDom Get a JSimpleXMLElement object from a DOM node.
loadFile Interprets an XML file into an object
loadString Interprets a string of XML into an object
setParser Set the parser
_characterData Handler function for the character data within a tag
_endElement Handler function for the end of a tag
_getStackLocation Gets the reference to the current direct parent
_handleError Handles an XML parsing error
_parse Start parsing an XML document
_startElement Handler function for the start of a tag

Related Classes

Examples

XML source document (simple.xml)

<?xml version=""1.0" encoding="utf-8" standalone="yes"?>
<document>
  <node>
    <child gender="m">Tom Foo</child>
    <child gender="f">Tamara Bar</child>
  <node>
</document>

Example 1: return XML source

// read and write a document
$xml = new JSimpleXML;
$xml->loadFile('simple.xml');
echo $xml->toString();

Will return the XML source of ‘simple.xml’ as depicted above.

Example 2:accessing node data

// access a given node's CDATA
echo 'Name:', $xml->root->node->child[0]->data();

// access attributes
$attr = $xml->root->node->child[1]->attributes();
echo 'Gender: ', ($attr['gender'] == 'f') ? 'female' : 'male';

Will output:

Name: Tom Foo
Gender: male

Example 3:Loop through child nodes

// access children
foreach( $xml->root->node->children() as $child ) {
    echo $child->data();
}

Will output:

Tom Foo
Tamara Bar

Discussion

Full name:
E-Mail:
 
references/joomla.framework/utilities/jsimplexml.txt (1910 views) · Last modified: 2007/08/15 15:13