php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52858 dom_import_simplexml() doesn't work on newly created SimpleXMLElement nodes
Submitted: 2010-09-16 05:51 UTC Modified: 2018-05-05 20:40 UTC
Votes:6
Avg. Score:3.8 ± 1.5
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:4 (100.0%)
From: tatarynowicz at gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.3 OS: Windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tatarynowicz at gmail dot com
New email:
PHP Version: OS:

 

 [2010-09-16 05:51 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-13 11:58 UTC] 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.
 [2018-05-05 20:40 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-05-05 20:40 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC