JSimpleXML
Extends \JObjectSimpleXML 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:
:simple.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
// read and write a document $xml = new JSimpleXML; $xml->loadFile('simple.xml'); print $xml->document->toString();
// access a given node's CDATA print $xml->root->node->child[0]->data(); // Tom Foo
// access attributes $attr = $xml->root->node->child[1]->attributes(); print $attr['gender']; // f
// access children foreach($xml->root->node->children() as $child) { print $child->data(); }
Note: JSimpleXML cannot be used to access sophisticated XML doctypes using datatype ANY (e.g. XHTML). With a DOM implementation you can handle this.
| package |
Joomla.Platform |
|---|---|
| subpackage |
Utilities |
| see | |
| since |
11.1 |
| deprecated |
12.1 Use SimpleXML instead |
Methods
__construct
Class constructor, overridden in descendant classes.
__construct(mixed $properties = null)
| since |
11.1 |
|---|
Arguments
- $properties
mixedEither and associative array or another object to set the initial properties of the object.
__toString
Magic method to convert the object to a string gracefully.
__toString() : string
| since |
11.1 |
|---|---|
| deprecated |
12.3 Classes should provide their own __toString() implementation. |
Response
stringThe classname.
_characterData
Handler function for the character data within a tag
_characterData(resource $parser, string $data) : void
| deprecated |
12.1 |
|---|---|
| since |
11.1 |
Arguments
- $parser
resourceThe XML parser.- $data
stringThe CDATA for the element.
_endElement
Handler function for the end of a tag
_endElement(resource $parser, string $name) : void
| deprecated |
12.1 |
|---|---|
| since |
11.1 |
Arguments
- $parser
resourceThe XML parser.- $name
stringThe name of the element.
_getStackLocation
Gets the reference to the current direct parent
_getStackLocation() : object
| since |
11.1 |
|---|---|
| deprecated |
12.1 |
Response
object
_handleError
Handles an XML parsing error
_handleError(integer $code, integer $line, integer $col) : void
| deprecated |
12.1 12.1 Use PHP Exception |
|---|---|
| since |
11.1 |
Arguments
- $code
integerXML Error Code.- $line
integerLine on which the error happened.- $col
integerColumn on which the error happened.
_parse
Start parsing an XML document
_parse(string $data = '') : void
Parses an XML document. The handlers for the configured events are called as many times as necessary.
| deprecated |
12.1 |
|---|---|
| see | |
| since |
11.1 |
Arguments
- $data
stringdata to parse
_startElement
Handler function for the start of a tag
_startElement(resource $parser, string $name, array $attrs = array()) : void
| since |
11.1 |
|---|---|
| deprecated |
12.1 |
Arguments
- $parser
resourceThe XML parser.- $name
stringThe name of the element.- $attrs
arrayA key-value array (optional) of the attributes for the element.
def
Sets a default value if not alreay assigned
def(string $property, mixed $default = null) : mixed
| since |
11.1 |
|---|
Arguments
- $property
stringThe name of the property.- $default
mixedThe default value.
Response
mixed
get
Returns a property of the object or the default value if the property is not set.
get(string $property, mixed $default = null) : mixed
| since |
11.1 |
|---|---|
| see | \JObject::getProperties() |
Arguments
- $property
stringThe name of the property.- $default
mixedThe default value.
Response
mixedThe value of the property.
getError
Get the most recent error message.
getError(integer $i = null, boolean $toString = true) : string
| since |
11.1 |
|---|---|
| see | \JError |
| deprecated |
12.3 JError has been deprecated |
Arguments
- $i
integerOption error index.- $toString
booleanIndicates if JError objects should return their error message.
Response
stringError message
getErrors
Return all errors, if any.
getErrors() : array
| since |
11.1 |
|---|---|
| see | \JError |
| deprecated |
12.3 JError has been deprecated |
Response
arrayArray of error messages or JErrors.
getParser
Get the parser
getParser() : resource
| deprecated |
12.1 Use SimpleXMLElement |
|---|---|
| see | |
| since |
11.1 |
Response
resourceXML parser resource handle
getProperties
Returns an associative array of object properties.
getProperties(boolean $public = true) : array
| since |
11.1 |
|---|---|
| see | \JObject::get() |
Arguments
- $public
booleanIf true, returns only the public properties.
Response
array
importDOM
Get a JSimpleXMLElement object from a DOM node.
importDOM(string $node, string $classname = null) : mixed
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.
| deprecated |
12.1 use simplexml_import_dom instead. |
|---|---|
| see |
http://www.php.net/manual/en/function.simplexml-import-dom.php |
| since |
11.1 |
Arguments
- $node
stringDOM document- $classname
stringcurrently ignored
Response
mixedJSimpleXMLElement or false if any errors occur
loadFile
Interprets an XML file into an object
loadFile(string $path, string $classname = null) : boolean
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.
| deprecated |
12.1 Use simplexml_load_file instead |
|---|---|
| see |
http://www.php.net/manual/en/function.simplexml-load-file.php |
| since |
11.1 |
Arguments
- $path
stringPath to XML file containing a well-formed XML document- $classname
stringcurrently ignored
Response
booleanTrue if successful, false if file empty
loadString
Interprets a string of XML into an object
loadString(string $string, string $classname = null) : 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.
| since |
11.1 |
|---|---|
| deprecated |
12.1 Use simpleXML_load_string |
| see |
http://www.php.net/manual/en/function.simplexml-load-string.php |
Arguments
- $string
stringWell-formed XML string data- $classname
stringcurrently ignored
Response
objectJSimpleXMLElement
set
Modifies a property of the object, creating it if it does not already exist.
set(string $property, mixed $value = null) : mixed
| since |
11.1 |
|---|
Arguments
- $property
stringThe name of the property.- $value
mixedThe value of the property to set.
Response
mixedPrevious value of the property.
setError
Add an error message.
setError(string $error) : void
| since |
11.1 |
|---|---|
| see | \JError |
| deprecated |
12.3 JError has been deprecated |
Arguments
- $error
stringError message.
setParser
Set the parser
setParser(resource $parser) : void
| deprecated |
12.1 Use SimpleXML |
|---|---|
| see | |
| since |
11.1 |
Arguments
- $parser
resourceXML parser resource handle.
setProperties
Set the object properties based on a named array/hash.
setProperties(mixed $properties) : boolean
| since |
11.1 |
|---|---|
| see | \JObject::set() |
Arguments
- $properties
mixedEither an associative array or another object.
Response
boolean
toString
Converts the object to a string (the class name).
toString() : string
Properties
_parser
The XML parser
| since |
11.1 |
|---|
Type(s)
resource
document
Document element
| since |
11.1 |
|---|
Type(s)
object
_stack
Current object depth
| since |
11.1 |
|---|
Type(s)
array
_errors
An array of error messages or Exception objects.