Forum Moderators: coopster

Message Too Old, No Replies

Creating XML with max file size

using php to build xml file of a max size

         

shudson250

10:59 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



This is a bit of a head scratcher for me... not sure its even possible.

I'm wanting to use PHP to build an XML file of max size of 100kb.

Is there any way that I can see how big the file is getting (as it is echo'ed an not output to a temp file) on the fly?

penders

11:23 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This would depend on what method you are using to generate your XML. If, for instance, you were using the SimpleXML [uk2.php.net] object then you could get the resulting XML as a string with $simplexml->asXML() [uk2.php.net] and then the strlen() [uk2.php.net] of this would be the number of bytes (assuming a single byte string - hhhmmm, will it be a single byte string?!).

shudson250

12:45 am on Feb 25, 2009 (gmt 0)

10+ Year Member



no, no simpleXML here. Just a Mysql search, followed by echo'ed xml headers and a while loop for <tags with attributes />.

penders

1:35 am on Feb 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, before you echo you could get the number of bytes/chars you are echo'ing and keep a running total?

Or, instead of echo'ing, keep appending to a string until you are above 100 x 1024 characters. Then echo the resultant string?

shudson250

1:43 am on Feb 25, 2009 (gmt 0)

10+ Year Member



aah okay. Any idea what the overhead would be for adding to the string size count roughly 500-1000 times?

shudson250

2:33 am on Feb 25, 2009 (gmt 0)

10+ Year Member



A bit clearer:
I will be creating (at the max) 500-1000 child nodes, so after each node is finished, I would count the new characters and see if it exceeded the limit, so the character count would be called each time a new child is added.

Any idea the overhead for something like that?

penders

10:47 am on Feb 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would have thought that the extra overhead of counting the number of chars you are writing and keeping a tally would be minimal when compared to building your nodes and echo'ing...?

shudson250

5:12 pm on Feb 26, 2009 (gmt 0)

10+ Year Member



ah, please forgive me. Still learning :)

For curiosities sake, what is the most efficient method for building an XML document?

penders

11:29 am on Mar 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For curiosities sake, what is the most efficient method for building an XML document?

I think this will depend on the size and complexity of your XML. PHP provides several built in objects for manipulating XML [uk2.php.net] and these will certainly help to maintain valid XML and simplify the creation of complex XML docs. However, objects like SimpleXML maintain the entire structure in memory (as you are doing by concatenating a string) so I can't imagine that working too well for massive XML docs (but again, that might depend on what you are using the XML for)!? If you just a require an XML snippet then I would guess that a bit of string building is about as efficient as it gets?