php SimpleXMLElement设置文本

时间:2010-06-03 23:44:14

标签: php xml

如何在php中为SimpleXMLElement设置文本?

3 个答案:

答案 0 :(得分:12)

假设$ node是一个SimpleXMLElement。这段代码:

renderElementLater

将在PHP 7中产生额外的childNode,而不是为$ node设置文本内容。使用:

$node->{0} = "some string"

代替。

答案 1 :(得分:5)

您是否看过基本的documentation examples

从那里:

include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

$xml->movie[0]->characters->character[0]->name = 'Miss Coder';

echo $xml->asXML();

答案 2 :(得分:2)

$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>

我在这里搜索这个答案的更多内容: