I don't understand the $parser resource in XML parsing. I've seen it used in every example e.g.

function startElement( $parser, $name, $attrs )

And all it ever seems to get used for is tracking the depth:

depth[$parser]++;

When I print out the $parser element, all I get is:-

Resource id #1

I don't understand what it's for, or if perhaps there's a way to use it to track which node is the current one. The problem I have is that the XML structure of the file is such that there are many nodes named the same:

<page>
    <page>
        <page />
        <page />
    </page>
</page>

So I can't use the name of the node as a reference. I notice that all the PHP/XML code examples solve the slightly simpler solution of parsing an XML file whose structure is known, but in my case I don't know how many <page>s there will be.

Alternatively I've heard of the DOM alternative. Anyone used this? Is it reliable? Why is it not included with PHP in the installation?

Thanks,
Antun

    I've seen this before but don't remember where. I think it was with mysql but it would be for same reason.

    is $parser an array?

      is $parser an array?

      Nope, because if it was, print_r( $parser ) would print it all out. Instead it gets used as an index for the $depth array.

        I know what tutorial your talking about , I saw it in the manual.

        I think the resource data type is new to php4, its kinda like a reference (pointer) to another resource (can contain methods properties, functions, constants whatever). In other languages, if you print the value of a reference (pointer) without de-referencing it you'll just get a hexidecimal value of its address in memory. I think that the php resource is similar, except it can point to more than just variables (in memory).

        Basically, $parser is whatever you make it, in this case its asigned a resource - an xml parser resource.

        so:
        $parser = xml_parser_create();

        $parser is now used as a resource handle to php's xml parser kinda like you would a file-handle to manipulate a file. So when you use functions like xml_parser_into_struct(), xml_parse(), xml_parser_set_option() etc..., you'll need to pass the resource id ($parser) to these functions so it can know which resource to use, set options to etc...

        I've been working on an XMLSocket class (in PHP) that will not only retrieve xml from a remote server but can also be used a an xml parser for organizing xml into a structure. If you (or anyone else) would like to test/check it out, I've just finished my first stable (I think 🙂 ) release. It has alot of good examples for you to check out.

          Write a Reply...