我是PHP,DOM和PHP DOM实现的新手.我正在试图做的是保存的根元素DOMDocument的$_SESSION变量,这样我可以访问它,修改它在随后的页面加载.
但是当我$_SESSION用来保存DOMElement的状态时,我在PHP中遇到错误:
警告:DOMNode :: appendChild()[domnode.appendchild]:无法获取DOMElement
我已经读过PHP DOMDocument对象无法原生保存$_SESSION.但是,可以通过保存DOMDocument的序列化来保存它(例如$_SESSION['dom'] = $dom->saveXML()).
我不知道是否同样适用DOMElement于将$_SESSION变量保存到变量,但这就是我所尝试的.我想要这样做的原因是使用带有一个附加属性的扩展类DOMElement.我希望通过在$ _SESSION中保存根DOMElement,我可以在以后检索元素并修改这个附加属性并执行测试,如果(additionalProperty === false){做某事; }.我还读到通过保存DOMDocument并稍后检索它,所有元素都作为来自本机DOM类的对象返回.也就是说,即使我使用扩展类来创建元素,我随后需要的属性也是不可访问的,因为保存对扩展类对象的引用的变量已超出范围 - 这就是为什么我'我正在尝试这件事.我首先尝试使用扩展类(不包括在下面),但是出现了错误......所以我恢复使用DOMElement对象来查看是否存在问题,但我仍然遇到相同的错误.这是代码:
<?php
session_start();
$rootTag = 'root';
$doc = new DOMDocument;
if (!isset($_SESSION[$rootTag])) {
$_SESSION[$rootTag] = new DOMElement($rootTag);
}
$root = $doc->appendChild($_SESSION[$rootTag]);
//$root = $doc->appendChild($doc->importNode($_SESSION[$rootTag], true));
$child = new DOMElement('child_element');
$n = $root->appendChild($child);
$ct = 0;
foreach ($root->childNodes as $ch) echo '<br/>'.$ch->tagName.' '.++$ct;
$_SESSION[$rootTag] = $doc->documentElement;
?>
Run Code Online (Sandbox Code Playgroud)
此代码提供以下错误(取决于我是直接使用appendChild还是使用importNode使用注释的代码行):
Warning: DOMNode::appendChild() [domnode.appendchild]: …
我遇到了一个似乎无法缩小范围的问题.在Zend Framework应用程序中,我使用Zend Cache来缓存自定义Response对象中包含的潮汐和天气数据.最初创建数据的点,一切正常.我序列化并缓存它.然后,当我点击刷新并从缓存中提取数据时,我收到以下错误:
消息:无法将字符串解析为XML
堆栈跟踪:
0 /home/cillosis/mysites/tidely/application/views/scripts/tides/location.phtml(38):SimpleXMLElement - > __ construct('')
1 /home/cillosis/mysites/tidely/library/Zend/View.php(108):include('/ home/cillosis/...')
2 /home/cillosis/mysites/tidely/library/Zend/View/Abstract.php(888):Zend_View - > _ run('/ home/cillosis/...')
...
这发生在我的视图中,我访问自定义"响应对象"中包含的XML,其中包含:
<div class="data-box">
<h3>Current Weather</h3>
<hr>
<?php
// *** THIS IS LINE 38 ***
$weather_XML = new SimpleXMLElement($this->response->_weatherdata->weatherResults);
$params = $weather_XML->data->parameters;
$img_path = $params->{'conditions-icon'}->{'icon-link'};
echo("<img src='".$img_path."'>");
...
Run Code Online (Sandbox Code Playgroud)
这是第一次运行时(缓存之前)对象的相关部分的转储:
["_weatherdata"]=>
object(Tidely_WeatherData)#79 (6) {
["weatherResults"]=>
string(6399) "<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">
...
</dwml> "
Run Code Online (Sandbox Code Playgroud)
你看到"......"的地方还有很多其他的XML数据.XML来自NWS(国家气象服务)API,我通过XML验证器运行它,它没有显示任何错误.一旦我序列化我的对象并缓存它就会发生问题.这是我如何设置Zend Cache:
// Setup caching
$frontendOptions = array('lifeTime' => 30, 'automatic_seralization' => …Run Code Online (Sandbox Code Playgroud)