|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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*/ PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 09:00:01 2025 UTC |
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}.> 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.