I am new to PHP, DOM and PHP DOM implementation. What I'm trying to do is save the root element DOMDocumentin a variable $_SESSIONso that I can access it and change it on subsequent page loads.
But I get an error in PHP when used $_SESSIONto save the state of DOMElement:
Warning: DOMNode :: appendChild () [domnode.appendchild]: Failed to get DOMElement
I read that the PHP DOMDocument object cannot be stored in the $_SESSIONoriginal. However, you can save it by preserving the serialization of the DOMDocument (for example, $_SESSION['dom'] = $dom->saveXML()).
I don't know if the same is true for storing a variable DOMElementin$_SESSIONbut this is what i tried. My reason for this is to use the extended DOMElement class with one additional property. I was hoping that by storing the root DOMElement in $ _SESSION, I could later retrieve the element and change this additional property and run a test, for example if (extraProperty === false) {do something; }. I also read that by storing a DOMDocument and then retrieving it, all elements are returned as objects from the native DOM classes. That is, even if I used the extended class to create the elements, the property that I later needed would not be available, because the link containing the link to the extended class object went out of scope - that’s why I'm trying this other thing. At first I tried to use an extended class (not included below), but got errors ...so I went back to using the DOMElement object to check if this problem is, but I still get the same errors. Here is the code:
<?php
session_start();
$rootTag = 'root';
$doc = new DOMDocument;
if (!isset($_SESSION[$rootTag])) {
$_SESSION[$rootTag] = new DOMElement($rootTag);
}
$root = $doc->appendChild($_SESSION[$rootTag]);
$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;
?>
( , appendChild importNode):
Warning: DOMNode::appendChild() [domnode.appendchild]: Couldn't fetch DOMElement in C:\Program Files\wamp_server_2.2\www\test2.php on line 11
Warning: DOMDocument::importNode() [domdocument.importnode]: Couldn't fetch DOMElement in C:\Program Files\wamp_server_2.2\www\test2.php on line 12
. -, ? , , , , "" DOM ? , XML. , DOM , DOMDocument , , DOMDocument /. DOMDocument. !
:
hakre . DOMElement , , . :
<?php
session_start();
$rootTag = 'root';
$doc = new DOMDocument;
if (!isset($_SESSION[$rootTag])) {
$root = new FreezableDOMElement($rootTag);
$doc->appendChild($root);
} else {
$doc->loadXML($_SESSION[$rootTag]);
$root = $doc->documentElement;
}
$child = new FreezableDOMElement('child_element');
$n = $root->appendChild($child);
$ct = 0;
foreach ($root->childNodes as $ch) {
$frozen = $ch->frozen ? 'is frozen' : 'is not frozen';
echo '<br/>'.$ch->tagName.' '.++$ct.': '.$frozen;
}
$_SESSION[$rootTag] = $doc->saveXML();
class FreezableDOMElement extends DOMElement {
public $frozen;
public function __construct($name) {
parent::__construct($name);
$this->frozen = false;
}
}
?>
Undefined property: DOMElement::$frozen. , saveXML loadXML, , FreezableDOMElement, DOMElement, frozen . ?