Edit report at https://bugs.php.net/bug.php?id=64440&edit=1

 ID:                 64440
 Updated by:         sala...@php.net
 Reported by:        matteosistisette at gmail dot com
 Summary:            SimpleXMLElement needs a way to add text content to
                     a node
 Status:             Open
 Type:               Feature/Change Request
 Package:            SimpleXML related
 Operating System:   all
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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!


Previous Comments:
------------------------------------------------------------------------
[2013-03-16 19:55:50] 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:03:23] sala...@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 18:28:46] 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*/



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64440&edit=1

Reply via email to