XML_Unserializer Options

XML_Unserializer Options – List of all XML_Unserializer options

Introduction to options

Options let you influence how XML_Unserializer treats the parsed XML. It allows you to define whether attributes should be parsed, whether to use associative arrays or objects for complex data types, and more.

Options can be passed as an associative array to the constructor of XML_Unserializer. You may also use setOption(), or setOptions() to set one or more options after the instance of XML_Unserializer has been created.

All available options

Here is a list of all options supported by XML_Unserializer.

XML_Unserializer options
XML_UNSERIALIZER_OPTION_ Name Alternate Key for $options Array Possible values Default Description
_COMPLEXTYPE complexType 'array' or 'object' 'array' Defines whether nested tags should be returned as associative arrays or objects.
_TAG_AS_CLASSNAME tagAsClass TRUE or FALSE TRUE Defines whether the tag name should be used as the class name if complexType is set to 'object'. If no class with the name of the tag exists, the class defined by 'defaultClass' is used.
_DEFAULT_CLASS defaultClass any string stdClass Defines the class that is used to create objects, if the complexType option is set to 'object'.
_ATTRIBUTE_KEY keyAttribute any string or array '_originalKey' If the attribute specified in this option exists for a tag, the value will be used as key or property name in the parent object or array. You may also specify an associative array if you want to use different key attributes for different tags. In this case, the array key contains the tag name and the array value the corresponding attribute name.
_ATTRIBUTE_TYPE typeAttribute any string '_type' If this attribute exists for a tag, the tag content will be converted to the specified type. Possible types are: string, integer, float, boolean, array, and object.
_ATTRIBUTE_CLASS classAttribute any string '_class' If XML_Unserializer creates an object, it will be an instance of the value specified with the defaultClass option, unless the tag has the attribute specified in this option. If it is set, the classname stored in the attribute value will be used.
_ATTRIBUTES_PARSE parseAttributes TRUE or FALSE FALSE With this option, you may tell XML_Unserializer to also parse the attributes of the tags. The next two options define how to treat the parsed attributes.
_ATTRIBUTES_ARRAYKEY attributesArray FALSE or any string FALSE If set to false, the attributes will be treated like nested tags. If you set it to a string, a new aray will be created and stored in the parent structure using key you specified in this option.
_ATTRIBUTES_PREPEND prependAttributes any string '' Allows you to specify a prefix for attribute names.
_CONTENT_KEY contentName any string '_content' If you decide to parse attributes or a tag contains cdata and tags, then the cdata will be stored in the index specified here.
_TAG_MAP tagMap associative array array() This allows you to map tag names to PHP classes. The names of the tags have to be in the keys and the values contain the class names to use for each tag.
_FORCE_ENUM forceEnum indexed array array() This allows you to specify a list of tags which will automatically be converted to an indexed array, independent of the number of repetitions of the tag. This can save you some IF conditions in your code.
_ENCODING_SOURCE encoding any valid encoding string (accepted by XML_Parser) null Defines the encoding of the original document.
_ENCODING_TARGET targetEncoding any valid encoding string (accepted by XML_Parser) null Defines the target encoding of the resulting data.
_DECODE_FUNC decodeFunction any valid PHP callback null This option allows you to define a callback function or method, that will be applied to all character data and attributes in the document before they are stored in the result. This allows you to decode any decoded data in the XML or convert all content to lowercase.
_RETURN_RESULT returnResult TRUE or FALSE FALSE If set to TRUE XML_Unserializer::unserialize() will return the result if the document could be unserialized instead of just TRUE.
_WHITESPACE whitespace OPTIONs _WHITESPACE_KEEP, _WHITESPACE_TRIM, or _WHITESPACE_NORMALIZE _WHITESPACE_TRIM Defines how whitespace in the document will be treated: keep it all, trim it, or normalize it.
_IGNORE_KEYS ignoreKeys any array array() List of tags whose contents will be passed to the parent tag instead of creating a new tag.
_GUESS_TYPES guessTypes TRUE or FALSE FALSE Whether or not to enable automatic type guessing for character data and attributes.
_OVERRIDE_OPTIONS overrideOptions TRUE or FALSE FALSE Allows you to override the options already set on this unserializer object.

Options via XML_UNSERIALIZER_OPTION Constants example

The following examples shows how to set options for XML_Unserializer, using the available XML_UNSERIALIZER_OPTION constants.

Using the constructor

<?php
require_once "XML/Unserializer.php";
$options = array(
    
XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'array'
);

$us = new XML_Unserializer($options);
$result $us->unserialize('example.xml'true);
?>

The following example shows how to set options for XML_Unserializer, using the available XML_UNSERIALIZER_OPTION constants, if the instance already has been created.

Using setOption() and setOptions()

<?php
require_once "XML/Unserializer.php";
$us = new XML_Unserializer();
$options = array(
    
XML_UNSERIALIZER_OPTION_TAG_MAP         => array( 'util' => 'XML_Util' ),
    
XML_UNSERIALIZER_OPTION_ATTRIBUTE_CLASS => '_classname'
);

$us->setOptions($options);
$us->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE'object');
$result $us->unserialize('example.xml'true);
?>

Options via $options examples

The following example shows how to set options for XML_Unserializer, using an $options array.

Using the constructor

<?php
require_once "XML/Unserializer.php";
$options = array(
    
'complexType' => 'array'
);

$us = new XML_Unserializer($options);
$result $us->unserialize('example.xml'true);
?>

The following example shows how to set options for XML_Unserializer, using an $options array, if the instance already has been created.

Using setOption() and setOptions()

<?php
require_once "XML/Unserializer.php";
$us = new XML_Unserializer();
$options = array(
    
'tagMap'         => array( 'util' => 'XML_Util' ),
    
'classAttribute' => '_classname'
);

$us->setOptions($options);
$us->setOption('complexType''object');
$result $us->unserialize('example.xml'true);
?>
XML_Unserializer - class to unserialize (read) XML documents (Previous) Constructor for XML_Unserializer (Next)
Last updated: Tue, 16 Jan 2018 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.