XML_PullParser
A token-based interface to the PHP expat XML library
version 1.3.2
Myron Turner
Appendix 1: Tokenized Arrays

Contents         

What follows are examples of the arrays returned by the XML_PullParser tokenizing functions. Unless otherwise indicated, they are based on the DNS example used in most of the code listings.

Notes on the structure of these arrays
1. These are "flat" arrays: they are not tree-structured, their members are addressed sequentially.
2. The S__ and E__ members of these arrays represent START and END tags.
3. The first member following the START tag is a an array of the element's attributes. If there are no attributes the array is empty. If there are attributes, they take the form of an associative array in which each key term is an attribute's name and its value term is the value of the attribute.
4. The character data follows the attributes array and it always marked as an associative array with a key named cdata. In large blocks of text marked up for conversion to other formats such (e.g. HTML), each set of mark-up tags has its own cdata section. XML_PullParser_getTextMarkedUP has the facility to handle mark up for HTML but could presumably be used for other types by subclassing.
5. Included here among the tokenizers are XML_PullParser_getChildren and XML_PullParser_getChildrenFromName. Their return type is not a tokenized array but instead a numerically indexed array of tokens.

XML_PullParser_getToken
 
        Array
        (
            [0] => S__ENTRY
            [1] => Array
                (
                )

            [2] => Array
                (
                    [cdata] =>

                )

            [3] => S__IPADDRESS
            [4] => Array
                (
                )

            [5] => Array
                (
                    [cdata] => 172.20.19.6
                )

            [6] => E__IPADDRESS
            [7] => Array
                (
                    [cdata] =>

                )

            [8] => S__DOMAIN
            [9] => Array
                (
                )

            [10] => Array
                (
                    [cdata] =>  example.com
                )

            [11] => E__DOMAIN
            [12] => Array
                (
                    [cdata] =>

                )

            [13] => S__SERVER
            [14] => Array
                (
                    [IP] => 192.168.10.1
                )

            [15] => Array
                (
                    [cdata] =>  example_1.com
                )

            [16] => E__SERVER
            [17] => Array
                (
                    [cdata] =>

                )

            [18] => S__SERVER
            [19] => Array
                (
                    [IP] => 192.168.10.2
                )

            [20] => Array
                (
                    [cdata] =>  example_2.com
                )

            [21] => E__SERVER
            [22] => Array
                (
                    [cdata] =>

                )

            [23] => S__SERVER
            [24] => Array
                (
                    [IP] => 192.168.10.3
                )

            [25] => Array
                (
                    [cdata] =>  example_3.com
                )

            [26] => E__SERVER
            [27] => Array
                (
                    [cdata] =>

                )

            [28] => S__ALIAS
            [29] => Array
                (
                )

            [30] => Array
                (
                    [cdata] =>  www.example.com
                )

            [31] => E__ALIAS
            [32] => Array
                (
                    [cdata] =>

                )

            [33] => E__ENTRY
        )

XML_PullParser_getElement("server")
        Array
        (
            [0] => S__SERVER
            [1] => Array
                (
                    [IP] => 192.168.10.1
                )

            [2] => Array
                (
                    [cdata] =>  example_1.com
                )

            [3] => E__SERVER
            [4] => S__SERVER
            [5] => Array
                (
                    [IP] => 192.168.10.2
                )

            [6] => Array
                (
                    [cdata] =>  example_2.com
                )

            [7] => E__SERVER
            [8] => S__SERVER
            [9] => Array
                (
                    [IP] => 192.168.10.3
                )

            [10] => Array
                (
                    [cdata] =>  example_3.com
                )

            [11] => E__SERVER
        )

XML_PullParser_getChild('server',$which,$token)
        Array
        (
            [0] => S__SERVER
            [1] => Array
                (
                    [IP] => 192.168.10.1
                )

            [2] => Array
                (
                    [cdata] =>  example_1.com
                )

            [3] => E__SERVER
        )

Notes for XML_PullParser_getChild
This function extracts the $which_th element and all its descendents.
See the class documentation for details.

XML_PullParser_getChildren and XML_PullParser_getChildrenFromName
        Array
        (
            [0] => Array
                (
                    [0] => S__SERVER
                    [1] => Array
                        (
                            [IP] => 192.168.10.1
                        )

                    [2] => Array
                        (
                            [cdata] =>  example_1.com
                        )

                    [3] => E__SERVER
                )

            [1] => Array
                (
                    [0] => S__SERVER
                    [1] => Array
                        (
                            [IP] => 192.168.10.2
                        )

                    [2] => Array
                        (
                            [cdata] =>  example_2.com
                        )

                    [3] => E__SERVER
                )

            [2] => Array
                (
                    [0] => S__SERVER
                    [1] => Array
                        (
                            [IP] => 192.168.10.3
                        )

                    [2] => Array
                        (
                            [cdata] =>  example_3.com
                        )

                    [3] => E__SERVER
                )

        )

Notes for XML_PullParser_getChildren and XML_PullParser_getChildrenFromName
See the discussion of these functions in the section on tokenized arrays.


Namespace Support
The following token was returned by XML_PullParser_getToken using the Movies document. It illustrates how namespaces are coded into each element's attribute array. Every element which has been asigned a namespace has an _ns_ attribute, which points to a single element associative array. This makes it possible to use PHP's built-in array_key_exists to test for namespace agreement, which is more precise and more efficient than string comparisons.

In the case of attributes found in the XML document, the namespace URI is prefixed to the attribute name with a verticle bar. Consequently attributes cannot be accessed by name directly, unless an accomodation is made for the namespace prefix.

A Token Illustrating Namespaces

Array
(
    [0] => S__MOVIE
    [1] => Array
        (
            [_ns_] => Array
                (
                    [HTTP://FEDORA.GEMINI.CA/LOCAL/] => 1
                )

        )

    [2] => Array
        (
            [cdata] =>

        )

    [3] => S__TITLE
    [4] => Array
        (
            [_ns_] => Array
                (
                    [HTTP://ROOM535.ORG/MOVIES/TITLE/] => 1
                )

        )

    [5] => Array
        (
            [cdata] => Gone With The wind
        )

    [6] => E__TITLE
    [7] => Array
        (
            [cdata] =>

        )

    [8] => S__DATE
    [9] => Array
        (
            [HTTP://ROOM535.ORG/MOVIES/DATES/|DAY] => 25
            [HTTP://ROOM535.ORG/MOVIES/DATES/|MONTH] => Apr
            [_ns_] => Array
                (
                    [HTTP://ROOM535.ORG/MOVIES/DATES/] => 1
                )

        )

    [10] => Array
        (
            [cdata] => 1939
        )

    [11] => E__DATE
    [12] => Array
        (
            [cdata] =>

        )

    [13] => S__LEADING_LADY
    [14] => Array
        (
            [_ns_] => Array
                (
                    [HTTP://ROOM535.ORG/MOVIES/STAR/] => 1
                )

        )

    [15] => Array
        (
            [cdata] => Vivien Leigh
        )

    [16] => E__LEADING_LADY
    [17] => Array
        (
            [cdata] =>

        )

    [18] => S__LEADING_MAN
    [19] => Array
        (
            [_ns_] => Array
                (
                    [HTTP://ROOM535.ORG/MOVIES/STAR/] => 1
                )

        )

    [20] => Array
        (
            [cdata] => Clark Gable
        )

    [21] => E__LEADING_MAN
    [22] => Array
        (
            [cdata] =>

        )

    [23] => E__MOVIE
)