The WebAbility® Network Developers - Documentation
WebAbility Site
\core\WASimpleXML Reference

I. Structure:


- namespace:
\core\

It is a static object
- Does not have superclass
- Does not have subclasses

This is a static class that contains an XML parser to compile a simple XML into a PHP array.
The parser does not make difference between information "before" and "after" children tags (see examples).
All the children are put together into a sub array of the father automatically.

There are 2 methods, compile and tags.


II. The simple compiler:


The first method in the static XML parser is the compile method.


For example the code:

<?xml version="1.0" encoding="UTF-8" ?>
<language id="daysofweek" lang="es">

<entry id="sunday"><![CDATA[Domingo]]></entry>

</language>


will result in:

array(
'tag' => 'language',
'attributes' => array ( 'id' => 'daysofweek', 'lang' => 'es' ),
'children' => array( 0 => array(
'tag' => 'entry',
'attributes' => array ( 'id' => 'sunday' )
'data' => 'Domingo'
))
)


The rules of construction are simple:
For each node, it will result into an array that contains:
- a 'tag' entry with the tag name
- an 'attribute' entry with the attributes into an array of 'id' => 'value', even if there is only one attribute.
- a 'data' entry if there is anything written into the tag. Content is trimed, so spaces and non visible characters are removed.
- a 'children' entry with Numbered entries for each children (0, 1, 2, etc)

'attribute' and 'data' entries are optionals, they will not be present if there is no attributes or data into the node.
If there is no children, there is no numbered entries either.

If there is anything writen before, between and after the children, anything will be concatened into the 'data' entry:


For example the code:

<?xml version="1.0" encoding="UTF-8" ?>
<language id="daysofweek" lang="es">
Some text before
<entry id="sunday"><![CDATA[Domingo]]></entry>
Some text between
<entry id="monday"><![CDATA[Lunes]]></entry>
And some text after
</language>


will result in:

array(
'tag' => 'language',
'attributes' => array ( 'id' => 'daysofweek', 'lang' => 'es' ),
'children' => array( 0 => array(
'tag' => 'entry',
'attributes' => array ( 'id' => 'sunday' )
'data' => 'Domingo'
),
1 => array(
'tag' => 'entry',
'attributes' => array ( 'id' => 'monday' )
'data' => 'Lunes'
)),
'data' => 'Some text before Some text between And some text after'
)


The simple XML is perfect for XML configuration files, XML languages files, XML database records files etc.

III. Tag-or-data structure


Another method in the static XML parser is the tags method. The tag-or-data algorithm is simple:
If the XML tag contains other tags, so the array will have those tags as children. Data will be ignored.
If the XML tag contains only data, so the array will contains this data information.
All the attributes are ignored.


For example the code:

<?xml version="1.0" encoding="UTF-8" ?>
<language id="daysofweek" lang="es">

<entry id="sunday"><![CDATA[Domingo]]></entry>

</language>


will result in:

array(
'language' => array ( 'entry' => 'Domingo' ),
)


If there are mixed tags and data, all the data will be ignored.
If there are various tags with the same name, a 0-indexed array will be returned.


For example the code:

<?xml version="1.0" encoding="UTF-8" ?>
<language id="daysofweek" lang="es">
Some text before
<entry id="sunday"><![CDATA[Domingo]]></entry>
Some text between
<entry id="monday"><![CDATA[Lunes]]></entry>
And some text after
</language>


will result in: (data text ignored)

array(
'language' => array (
'entry' => array('Domingo', 'Lunes')
)
)



The tag-or-data is best to represent a hierarchical array of data.
For example:



<?xml version="1.0" encoding="UTF-8" ?>
<config>
<servertype>Standalone</entry>
<buffersize>4096</entry>
<hostname>www.webability.info</entry>
</config>


will result in:

array(
'config' => array (
'sertertype' => 'Standalone',
'buffersize' => 4096,
'hostname' => 'www.webability.info'
)
)



IV. Use:


Just call the compile static method with the XML string:



$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<language id="daysofweek" lang="es">

<entry id="sunday"><![CDATA[Domingo]]></entry>

</language>
XML;

var_dump( \core\WASimpleXML::compile($xml) );
or
var_dump( \core\WASimpleXML::tags($xml) );





V. Reference:


5.1. Contructor:

none

5.2. Constants:

none

5.3. Attributes:

none

5.4. Methods:


public array static compile($xml)
This method compiles an XML file into a ready-to-use array of values.
  • $xml: XML valid string to parse with the propper syntax.

public array static tags($xml)
This method compiles an XML file into a tag-or-data array of values.
  • $xml: XML valid string to parse with the propper syntax.