0
votes

I receive very large xml files which need spot edits (adding elements at particular nodes). The file size makes using DOMDocument or SimpleXML not an option. I'm using XMLReader to parse the file but it appears that my only option is to completely rewrite the file using XMLWriter. I'm curious if there is a better way? Perhaps something that lets me edit the file at the cursor location of XMLReader?

Ideally I'd be doing this on the fly with PHP but I make it work doing a one off with C# to get things up and running if there is a tool that makes this work more efficiently.

1
XmlReader in, XmlWriter out is definitely the best way. What's your concern with going that route? - Brian Warshaw
I don't want to make 2 files (the one I'm reading from and the new file that I have to create with XMLWriter containing my edits). I have this GB file and I want to make spot edits using a file handle to a line in the file ideally. Is there a file reader that will let me do line edits? I could read line by line into a DOMDocument I guess. - Oddible
For what you're trying to do, your have to duplicate the file--either in memory with DOM or on disk with XmlWriter. I'm not aware of any reading method that allows you to edit the original file in place. Honestly, it's just better not to, anyway. If you screw up, or if something unexpected goes wrong, you'd be corrupting the original file. That's a particularly bad scenario when dealing with something like XML. - Brian Warshaw

1 Answers

1
votes

If the edits can be expressed using XSLT, I would recommend this since XSLT parsers optimize the query and in most cases perform a single run on the file...

XSLT is a language (in XML) to describe how to rewrite an XML document into another one. Most XSLT parsers are highly optimized (in time, memory and disk usage).