0

I need to read xml file with PHP,but xml file have many record update. Can I read only 10 record last update ?

Thank you very much.

iDev Man
  • 39
  • 3
  • 12
  • Please post the relevant parts of the XML file and explain your problem more clearly. – jackbot Apr 28 '11 at 07:49
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file). I assume you know how to use a `for` loop. – Gordon Apr 28 '11 at 07:55
  • corresponds "last update" to "added at a specific position" ?? For example, are the newest entries always at the start/end of the xml file? – Yoshi Apr 28 '11 at 08:02

1 Answers1

0

In this case, you should write your own reader function/class/whatever. Pseudo-code (its a little kind of pythonic maybe):

stop_counter = 10 // 20, 30.... etc
file = open xml_file
put cursor in the begging of the file
xmlpart = ''
counter = 0
while (not end of file)
  line = read single line from file
  xmlpart += line
  if (end of xml entry AND ++counter == stop_counter) // we can tell it by closed tag in line
    break

And then you just parse this part of xml. Pretty easy.

Nemoden
  • 8,816
  • 6
  • 41
  • 65
  • what makes you think lines in a file correspond to XML records in the file? And why do you iterate over the lines in the file at all instead of using a proper XML parser right from the start? – Gordon Apr 28 '11 at 07:58
  • You are right. Not a perfect answer. This approach saved me plenties of memory, but I'm dealing with XML files structured one-XML-record-each-line. "instead of using a proper XML parser right from the start". Because in this case you have to load the whole XML into memory and you might overuse it. I work on high-loaded system where it is absolutely vital to use as less memory as possible, so sometimes I have to invent unusual approaches. What I've described is exactly what I used. I just assumed maybe @KOKARAT is looking for something like this since he's noticed that his XML file is huge. – Nemoden Apr 28 '11 at 08:36
  • Anyway, my method is simply rewritable to read not line-by-line, but by certain amount of bytes. It is easy to figure out how to implement it. I just provided the very basic idea of how it might be done - not to read the whole file, but get exactly as many records, as you need. – Nemoden Apr 28 '11 at 08:39
  • If memory is an issue, a parser loading the XML into memory isnt "proper". PHP has pull- and event-based parsers available with XMLReader and XMLParser. Those would be proper for memory-intensive scenarios. – Gordon Apr 28 '11 at 08:59
  • Honestly, I have not even used neither of XMLReader or XMLParser. I widely used SimpleXML (which loads the whole file). Maybe you should just suggest your answer using XMLParser to @KOKARAT ? And I will take look at XMLParser and XMLReader - probably I should better use them rather then inventing the wheel. – Nemoden Apr 28 '11 at 09:17
  • 1
    Nah, the question is a duplicate in my book, so I wont answer. But have a look at my answer to [Best XML Parser for PHP](http://stackoverflow.com/questions/188414/best-xml-parser-for-php/3616044#3616044) for some more information on the topic. Make sure you follow the links. – Gordon Apr 28 '11 at 09:29
  • I will consider it. Thank you. – Nemoden Apr 28 '11 at 10:07
  • Sorry for my English, if make every body not clear in my question. example : http://rssfeeds.s3.amazonaws.com/goldbox the feed consist many items but I need just only top 10 records newest updated. please advide. Thank you. – iDev Man Apr 28 '11 at 10:54