Bug #52858 [Opn->Nab]: dom_import_simplexml() doesn't work on newly created SimpleXMLElement nodes
Edit report at https://bugs.php.net/bug.php?id=52858&edit=1
ID: 52858
Updated by: requinix@php.net
Reported by: tatarynowicz at gmail dot com
Summary: dom_import_simplexml() doesn't work on newly created
SimpleXMLElement nodes
-Status: Open
+Status: Not a bug
Type: Bug
Package: SimpleXML related
Operating System: Windows 7
PHP Version: 5.3.3
Block user comment: N
Private report: N
New Comment:
As hinted at, ->three does not exist in the XML but SimpleXML will give something to pretend that
it does - perhaps it could sometimes exist in the document but the one it parsed did not contain
one. SimpleXML cannot actually create a <three> because that would necessarily modify the
document... which in this case is wanted, yes, but in the general case should not happen.
It would be nice if MyXML could add detached copies of itself to the intended document, but being
separated from the document means it can't do that, so since SimpleXMLElement doesn't use
magic methods like __get that means @hanskrentel's advice is the only recourse I can see.
Closing NAB also due to age.
Previous Comments:
------------------------------------------------------------------------
[2013-02-13 11:58:47] hanskrentel at yahoo dot de
Please compare what you do with:
$xml->three->addChild('alpha');
You will get a warning:
Warning: SimpleXMLElement::addChild(): Cannot add child. Parent is not a permanent member of the XML
tree
If you extend from SompleXMLElement you probably want to add a similar warning for your function.
You can check for the condition this way:
if ($this[0] == NULL) {
$where = __CLASS__ . '::' . __FUNCTION__ . '(): ';
$what = 'Cannot add child. Parent is not a permanent member of the XML tree';
trigger_error($where . $what, E_USER_WARNING);
return;
}
I hope this is helpful and makes the bigger picture more clear that this is how SimpleXMLElement
works.
You can however create your own type that does know how to add this on the fly, however due to the
magic nature of SimpleXMLElement, it is not possible as long as you extend from
SimpleXMLElement.
It requires you to create a decorator and write code for that part of the magic your own and
delegate the rest to the SimpleXMLElement decoratee / subject.
------------------------------------------------------------------------
[2010-09-16 05:51:22] tatarynowicz at gmail dot com
Description:
------------
The problem is readily apparent when you run the test script. When
SimpleXMLElement dynamically creates a child element, and you directly call a
method on the child element, the $this context is apparently not a proper
SimpleXMLElement, i.e. dom_import_simplexml() does not accept it as input.
class MyXML extends SimpleXMLElement {
public function cdata($text) {
dom_import_simplexml($this);
}
}
$xml = new MyXML('<foo/>');
$xml->three->cdata('Three');
Test script:
---------------
<?php
class MyXML extends SimpleXMLElement {
public function cdata($text) {
$node = dom_import_simplexml($this);
$owner = $node->ownerDocument;
$node->appendChild($owner->createCDATASection($text));
return $this;
}
}
$xml = new MyXML('<foo/>');
// works
$xml->one = 'One';
// also works
$xml->two = '';
$xml->two->cdata('Two');
// doesn't work
$xml->three->cdata('Three');
print $xml->asXML();
Expected result:
----------------
<?xml version="1.0"?>
<foo><one>One</one><two><![CDATA[Two]]></two><three><![CDATA[Three]]></three></foo
>
Actual result:
--------------
Warning: dom_import_simplexml(): Invalid Nodetype to import in
C:\WWW\Work\WC\www\dom.php on line 5
Notice: Trying to get property of non-object in C:\WWW\Work\WC\www\dom.php on line
6
Fatal error: Call to a member function appendChild() on a non-object in
C:\WWW\Work\WC\www\dom.php on line 7
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=52858&edit=1
Thread (3 messages)