消息“不允许对'SimpleXMLElement'进行序列化”的未捕获异常'Exception' [英] Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'

查看:505
本文介绍了消息“不允许对'SimpleXMLElement'进行序列化”的未捕获异常'Exception'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么会这样。我不是 序列化XML,而是从RSS feed创建的数组(请注意,这只是一个片段):

I am not sure why this is coming up. I am not serializing the XML, but my array that I created from an RSS feed (note this is just a snippet):

$game_data = array (
    'sysreqos'  => $game->systemreq->pc->sysreqos,
    'sysreqmhz' => $game->systemreq->pc->sysreqmhz,
    'sysreqmem' => $game->systemreq->pc->sysreqmem,
    'sysreqdx'  => $game->systemreq->pc->sysreqdx,
    'sysreqhd'  => $game->systemreq->pc->sysreqhd,
);

然后我将其序列化 $ some_var = serialize($ game_data)并写入文本文件 fputs($ fh,$ some_var)

Then I serialize it $some_var = serialize($game_data) and write to a text file fputs($fh,$some_var).

但是没走那么远,它会在序列化行上出错:

But it does not get that far, it errors out on the serialize line:


未捕获的异常 Exception,消息为 SimpleXMLElement的序列化

Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'


推荐答案

您必须将XML数据转换为字符串,因为在内部它们都是 SimpleXMLElement s。

You have to cast the XML data to a string because internally they are all SimpleXMLElements.

$game_data = array (
                'sysreqos'       => (string)$game->systemreq->pc->sysreqos,
                'sysreqmhz'      => (string)$game->systemreq->pc->sysreqmhz,
                'sysreqmem'      => (string)$game->systemreq->pc->sysreqmem,
                'sysreqdx'       => (string)$game->systemreq->pc->sysreqdx,
                'sysreqhd'       => (string)$game->systemreq->pc->sysreqhd
            );

或者更优雅一点:

$game_data  = array();
$properties = array('sysreqos', 'sysreqmhz', 'sysreqmem', 'sysreqdx', 'sysreqhd');
foreach ($properties as $p) {
    $game_data[$p] = (string)$game->systemreq->pc->$p;
}

这篇关于消息“不允许对'SimpleXMLElement'进行序列化”的未捕获异常'Exception'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆