(PHP) How do I get XMLReader behave like SimpleXML with Xpath? (Large directory like XML file)

Posted by AESM on Stack Overflow See other posts from Stack Overflow or by AESM
Published on 2010-05-28T20:39:47Z Indexed on 2010/05/28 20:42 UTC
Read the original article Hit count: 133

Filed under:
|
|
|

So, I've got this huge XML file (10MB and up) that I want to parse and I figured instead of using SimpleXML, I'd better use XMLReader. Since the performance should be way better, right? But since XMLReader doesn't work with XPath...

The xml is like this:
<root name="bookmarks">

* <dir name="A directory">  
    <link name="blablabla">  
    <dir name="Sub directory">  
        ...
    </dir>
* </dir>  
* <link name="another link">

</root>

With SimpleXML combined with Xpath,
I would simple do like:

$xml = simplexml_load_file('/xmlFile.xml');
$xml->xpath('/root[@name]/dir[@name="A directory"]/dir[@name="Sub directory"]');

Which is so simple.
But how do I do this with just using XMLReader?

Ps.
I'm converting the resulting node to DOM/SimpleXML to get its inner contents.
Like this:

$node = $xr->expand();
$dom = new DomDocument();
$n = $dom->importNode($node, true);
$dom->appendChild($n);
$selectedRoot = simplexml_import_dom($dom);

Is this ok?

...

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about xpath