开发者

How can I handle a huge XML file using SimpleXML but to prevent memory and performance problems?

I am trying to avoid XMLReader for an app I build that has a huge XML file. SimpleXML is easy to write and I was wondering if there is any开发者_如何转开发 way to successfully handle it (memory and performance issues) in a quite busy server. What I will do, is to echo some data from that XML mainly from a search form.


Ok, if you really want to do this without XMLReader, here's what you could do.

Use fopen to open and read N number of bytes of that file.

Fix the ending : (That's the tough part but it's perfectly doable) You do it by closing anything left unclosed and also if needed backtracking if you happen to be in the middle of some text.

When that XML chunk is finally valid you can parse it with simplexmL.

Process that chunk or save it in its separate XML file and create another chunk ...until you have all of them.

Obviously if your XML is complex this might get a little painful.

Summary : By creating your own custom/dirt-cheap xml parser/fixer you can split a huge XML file into multiple smaller files.


If your file is mostly a lot of similar nodes, like a big list of books where the number of books is large but the book record itself is small, you could use a variation of smaura's answer by using XMLReader to walk through each node, then convert the node to an XML string and pass it to SimpleXML. That way, you're using a streaming solution for the big list, but once you get each record you get the benefits of easily accessing the record with SimpleXML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜