Mysterious error 'Node no longer exists'

yelvington - December 9, 2007 - 20:21

I'm posting this solution in advance for the benefit of others who might trip over this SimpleXML issue.

Drupal 6 automatically passes cache_save and cache_get arguments through PHP's serialize/unserialize functions.

If you parse an XML string using SimpleXML, it creates an object with invisible properties that won't show up if you do a print_r().

This object is a built-in object in PHP terminology, and should not be serialized/unserialized. See the discussion here: http://us3.php.net/manual/en/function.serialize.php

In my case I was trying to store a string, but unknowingly I was storing an object. I had parsed an RSS feed using SimpleXML and extracted one element like so:

$status = $statusobject->channel->item[0]->title;

The resulting $status was printable as a string but actually, unbeknownst to me, was an object when passed through D6's cache functions. When D6 attempted to unserialize the stored value, PHP threw an error: "Node no longer exists."

A cast fixed the problem:

$status = (string) $statusobject->channel->item[0]->title;

Thanks for the information.

yaph - July 7, 2008 - 18:14

Thanks for the information. I should have searched the Drupal forum first after running into this problem. So instead of trying to cache the simpexml object I cache the XML string I receive from an API request and now all works fine.

--
Websites: SEO-Expert-Blog.com | Torlaune.de

@yelvington - awesome

Gregory Go - December 17, 2008 - 03:27

@yelvington - awesome troubleshooting! Thanks for writing this up. "Mysterious error" is a perfect description. :)

Alternate way to save string: toXML()

rfay - February 2, 2009 - 11:42

Thanks for saving the day with this post. I definitely spent several hours beating my head against this before finding your note.

An alternate way to deal with a SimpleXML object or a piece of one is to use the SimpleXML method asXML() to deserialize it before caching:

<?php
// $xmlobject is an object of type SimpleXML
$text = $xmlobject->asXML();
cache_set($key,$text);

// And later retrieve with:
$xmltext = cache_get($key);
$retrieved_object = new SimpleXMLElement($xmltext);

// And now $retrieved_object will be the same as $xmlobject
?>

 
 

Drupal is a registered trademark of Dries Buytaert.