0

I am dealing with huge XML files (hundreds of MBs). I found perfect solution that does work for me: How to use XMLReader in PHP? (accepted answer).

Now, in my code I need to know the path of currently processed node. I am using DOM function getNodePath for that purpose. Everything looks OK unless XML document has declared default NS.

Here is my code:

$doc = new DOMDocument();
$z = new XMLReader;

$z->open($_SERVER['argv'][1]);

while ($z->read() && $z->name !== 'header');
$onix = simplexml_import_dom($doc->importNode($z->expand(), true));

$xpath = dom_import_simplexml($onix)->getNodePath();
echo "#".$xpath."#".PHP_EOL;

And here go the sample files. First - working - without NS declared, and the 2nd is namespaced.

<ONIXmessage>
<header>
    <m174>blah</m174>
</header>
</ONIXmessage>


<ONIXmessage xmlns="http://www.editeur.org/onix/2.1/short">
<header>
    <m174>blah</m174>
</header>
</ONIXmessage>

I need to get "/header" in my code. But I get "/*" if I parse the 2nd file. Removing xmlns attribute using string functions works, but it is not acceptable for me.

Community
  • 1
  • 1
premax
  • 9
  • 1
  • 1
    Could you provide a bit more content to your XML sample and why you need the xpath of it? The sample you posted is rather simple to get info without having to rely on grabbing the xpath of it. – Prix Jul 02 '14 at 23:47
  • The question is not why do I need xpath, but how to get it. Imagine XML file that is 500MB huge and has tags that are nested dozen times. It is an ONIX file and I have a working algorithm using recursive function to import the data to relational database. What you see here is a small cut from the algorythm. – premax Jul 03 '14 at 19:48

0 Answers0