Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minify XML with PHP?

I have a php script that reads an XML file and modifies it with SimpleXML. After all the modifications are done script saves the file (size ~ 2.8 mb).
I need to load this XML file with jQuery and it takes quite some time. Is there a way to compress/minify my XML file to reduce loading time in my AJAX call.

UPDATE:

The XML file contains SVG for the webpage (large metabolic map) therefore it has to preserve all the contents of XML-nodes.

like image 710
lanan Avatar asked Feb 23 '23 08:02

lanan


1 Answers

EDIT The OP made clear that this is about an SVG file after I wrote my answer.


Don't transport 2.5MB of XML to the client if you do not absolutely need all of it on the client (and I doubt you do). A better strategy is to use the XML file like a database:

  • Create a proxy page in PHP that accepts XPath expressions and returns relevant parts of the XML only.
  • Use jQuery to issue Ajax requests that fetch those relevant parts when it becomes necessary (i.e. when the user triggers an action).
  • Use memcached or another caching technique to prevent full XML parsing on the server for every request.
  • Depending on your application use profile, use memcached to cache individual Ajax responses, as well. Additionally, set HTTP caching headers so that the client does not re-request stuff that is still valid.
  • Enable gzip compression for the PHP Ajax responses to save response time and bandwidth.

It's a little more work that way. but it will boost speed — probably by several orders of magnitude.

like image 97
Tomalak Avatar answered Mar 06 '23 15:03

Tomalak