سؤال

I want to save a piece of XML code in the customer session. So far I tried with the following code.

// Get customer session. 
$customerSession = Mage::getSingleton('customer/session');

// $xml contains a piece of XML code.
$xml= new SimpleXMLElement($xml);
$customerSession->setSegmentList($xml);

But I'm getting the following error.

String could not be parsed as XML

Can anyone suggest how to resolve this? How can I save XML piece of code in customer session?

هل كانت مفيدة؟

المحلول

You cannot save objects in session. Well you can but it's a bit complicated. You have to serialize them first and make sure that all the objects within an object are serilizable.
But in your case it's easier.
You can save it in session as a string.
Then just convert it to SimpleXMLElement after reading it.

$xml = '<root><node>text</node></root>';
$customerSession->setSegmentList($xml);

and later when you need it.

$xml = $customerSession->getSegmentList($xml);
$xml = new SimpleXMLElement($xml);
//apply modifications to the xml object if needed.  
//then if you need to save it again in session...
$xml = $xml->asXML(); //turn to string again
$customerSession->setSegmentList($xml);

نصائح أخرى

another option would be to serialize your XML. In PHP you can always easily serialize/unserialize objects to store them as text.

$xml=new SimpleXMLElement($xml);
$customerSession->setSegmentList(serialize($xml));

later on when retrieving

$xml=unserialize($customerSession->getSegmentList());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top