php SimpleXMLElement set text
how to set text for a Simp开发者_开发问答leXMLElement in php?
Let's say $node is an SimpleXMLElement. This code:
$node->{0} = "some string"
will result in an extra childNode in PHP 7, instead of setting the text content for $node. Use:
$node[0] = "some string"
instead.
Did you look at the basic documentation examples?
From there:
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);
$xml->movie[0]->characters->character[0]->name = 'Miss Coder';
echo $xml->asXML();
$xml = SimpleXMLElement('<a><b><c></c></b></a>');
$foundNodes = $xml->xpath('//c');
$foundNode = $foundNodes[0];
$foundNode->{0} = "This text will be put inside of c tag.";
$xml->asXML();
// will output <a><b><c>This text will be put inside of c tag.</c></b></a>
More on my search of this answer here:
- How can I set text value of SimpleXmlElement without using its parent?
精彩评论