14 comments
Brian Moon Says:
What purpose would asXML serve for caching the data? I don't want to parse it again.
Dave Marshall Says:
I'm not sure I follow. Are you saying you cache a SimpleXML object?
Can't you just cache $simplexml->asXML()?
Brian Moon Says:
Well we don't want to cache the whole XML document. We are
only interested in part of it. I hate XML. The less I have to work with it the better.
ChieftainY2k Says:
"...We were caching the data in memcached which serializes the variable..."
Keep in mind that it is not possible to serialize PHP built-in object(s), which SimpleXML is :-)
Brian Moon Says:
Yeah, ChieftainY2k, that is kind of the point of my post. The serialization does not work and I get errors. I am not sure what your point is.
speedmax Says:
Wow, I was using exactly the same hack 4 months ago in the system i built.. until try to deploy onto a PHP5.1 environment.
Then I find the Set::reverse(Object) method in cakephp to convert any objects into array.. the magically Set class
Brian Moon Says:
Hmm, one function, json hack or load a huge bloated framework. I think I will stick with my functions. You realize that the CakePHP solution is only doing the same thing right? It is not magical.
Antti Says:
No chance using option LIBXML_NOBLANKS when loading up the XML?
Dar Ksyte Says:
I started to play about with this and immediately fell into a hole.
I started with a few lines of XML which includes an empty tag,
and then made my object with simple_xml_loadstring.
Dumping the object back to string with XMLobj->asXML() gave me a mangled tag, just
Other tags (with values) survived OK. Something obvious I'm missing? PHP5.
Dar Ksyte Says:
Ah! I'd forgotten that there is a shortform notation for a null item, so asXML() is just tidying up my original.
SneakyWho_am_i Says:
Sadly I don't think there is a faster way to do what you're doing.
Not that I'm going to dig around in the source, I'm too lazy for that (I haven't struck a problem YET)...
Sure, CakePHP is just doing the exact same thing as what you are (not that I've pulled that to bits) but of course if SpeedMax is using Cake then it's right and proper for him to use its existing method (provided it suits his purpose) and not reinvent the wheel, right?
I was even tempted to print the value of the object's node out into a buffer and then capture the buffer (because buffers are strings) but that feels like a big, stupid waste of time and effort more than anything else.
Would probably work to stringify it though (then you can buy the "I've done that thing" tee shirt) because of course you can have nested buffers in PHP
Another thing (of course you've tried this) might be:
header ('Content-Type: text/plain');
$object = simplexml_load_string($data);
Reflection::export(new ReflectionClass($object)); //YAY!!
Anyway good luck finding the quicker way to get that data out (in?). Maybe my stupid rambling about nothing might give you some crazy idea about how to magically beat that object into the desired type.
SneakyWho_am_i Says:
Just to spam (sorry):
===================================================
Reflection::export(new ReflectionClass($filename->filename->filename->filename));
===================================================
It complained that I'd tried to do a reflection on something that wasn't an object. Then it went mental and threw a stack trace.
Interesting.
isa Says:
The simple and stupid solution I've found for this is to use the substr() function with no parameters: $load_array[$i]->date = substr($simplexml->xml_child, null);
For no reason, this converts the simplexml element into the string data type... wheeeee.
Comments are disabled for this post.



Dave Marshall Says:
Can't you run asXML() on any node in a SimpleXML object?