/ TroubleShooting

Serialization of 'SimpleXMLElement' is not allowed

PHP 中建立 XML 的寫法大概是這樣

$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

$currentTrack = $domtree->createElement("Root");
$currentTrack = $xmlRoot->appendChild($currentTrack);
$currentTrack->appendChild($domtree->createElement('key','value'));
echo $domtree->saveXML();

Parse XML 字串成 Object 是

$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8' ?>
<Root><key>value</key></Root>");
echo $xml->key;  // 這行可能會出錯

有些情況下 Parse XML 的 Object Element 在存取時會出現「Serialization of 'SimpleXMLElement' is not allowed」,這時只要強制轉行成 String 即可

echo (string)$xml->key;