1

Is there a way to loop through an XML file from a specific element / node.

For instance if I want to start from <offer id="a2a9d7a3a520de69e8e06f3e53df1c49"> in this XML feed: http://pastebin.com/n5myzcz1

Is is then possible to load all offers after that ID?

Simon Thomsen
  • 1,351
  • 7
  • 27
  • 37

1 Answers1

0

I was going to suggest XPath but since you mention that the file is 1GB, you should use a streaming parser such as XMLReader (which I now read that you are already using).

You are bound to have a linear search because you don't know where the element you want is so you have to go through all of them until you find it.

EDIT

Just an esoteric idea could be using shell_exec to grep the file to find the line where the ID is and then cutting the file from that line using sed or equivalent.

EDIT 2

Ahhhh, this was fun!!

$line = intval(shell_exec('grep -n a2a9d7a3a520de69e8e06f3e53df1c49 orders.xml | cut -d : -f1'));
$totalLines = intval(shell_exec('wc -l orders.xml'));
echo shell_exec("sed -n '".$line.",".$totalLines."p' orders.xml");

Maybe you can just put 9999999999 instead of caculating the total lines. Not saying you should of course ;) ;)

I don't know if this is faster than just going through the file with XMLReader but I guess it's up to you to decide if it's worth it.

I hope this can give you further ideas to solve your problem.

Ricardo Velhote
  • 4,630
  • 1
  • 24
  • 30