php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64440 SimpleXMLElement needs a way to add text content to a node
Submitted: 2013-03-16 18:28 UTC Modified: 2013-03-16 22:33 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: matteosistisette at gmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-03-16 18:28 UTC] matteosistisette at gmail dot com
Description:
------------
There is currently no way (except for this horrible hack 
http://stackoverflow.com/questions/3153477/how-can-i-set-text-value-of-
simplexmlelement-without-using-its-parent) to add or modify the text content of a 
xml node with SimpleXMLElement.

It needs methods called somethig like addTextContent() or setTextContent() or 
addTextNode() or whatever.


Now please don't tell me this is a feature request. This is a huge design flaw. 
This is the lack of an obvious, vital feature. 

Test script:
---------------
$xml_a=new SimpleXMLElement("<data/>");
$xml_b=new SimpleXMLElement("<data>someText</data>");

//Now try to modify $xml_a so that it becomes like $xml_b 
//It's impossible.

/* You would need something like:

  $xml_a->addContent("someText");
or
  $xml_a->addTextNode("someText");
or
  $xml_a->setContent("someText");

No way you can accomplish that*/


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-16 19:03 UTC] salathe@php.net
What's wrong with $element[0] = "someText" ?

It is also not very often that one wishes to edit the documentElement's text content.  When editing sub-elements the following can be 
used when there is only one matching "<child>":

    $element->child = 'some text';

If there are multiple "<child>" elements, then you should specify which one to change:

    $element->child[2] = 'some text';

There is no requirement to use the "horrible hack" syntax: $element->{0}.
 [2013-03-16 19:03 UTC] salathe@php.net
-Status: Open +Status: Feedback -Type: Bug +Type: Feature/Change Request
 [2013-03-16 19:55 UTC] matteosistisette at gmail dot com
> What's wrong with $element[0] = "someText" ?

Oh, 
- that it is not documented (which is why i had no idea it existed)
- that it is not object-oriented (which makes it even difficult to document: 
there's no proper place in documentation where one would expect to find it)


> When editing sub-elements the following can be 
used when there is only one matching "<child>":
    $element->child = 'some text';

Yes, you need a reference to the parent, however, And this means you can't do 
that with the root element.


Example use: you create an element, and depending on some condition you decide 
whether to leave it empty or put content into it.
 [2013-03-16 19:55 UTC] matteosistisette at gmail dot com
-Status: Feedback +Status: Open
 [2013-03-16 22:33 UTC] salathe@php.net
I agree completely that the documentation of the currently available features 
could do with a little improvement and extension, with even more examples of 
SimpleXML's often magical-seeming syntax. However, that is going beyond the realm 
of this "bug" / feature request.

I can see use for a new method which sets the text of an element, the current 
ways are a little eyebrow raising to say the least!
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Nov 15 20:01:39 2019 UTC