有没有办法附加一个 SimpleXML 元素的子元素?
例如
<comments>
<comment1>
<commentcontent>Test</commentcontent>
</comment1>
<comment2>
<commentcontent>Test2</commentcontent>
<comment3>
<commentcontent>Test3</commentcontent>
</comment3>
</comment2>
</comments>
我希望comment3 能够对其进行子评论。我在想我可以使用一些正则表达式或通配符来忽略评论元素的数量,但还没有弄清楚如何。
我在尝试
$commentlevel = calculatelevel(($level[$commentkey] - 1)) . 'Comment' . $parent[$commentkey];
$newcomment = $commentsxml->xpath($commentlevel)->addChild('Comment' . $id);
function calculatelevel($level) {
$compile = '';
for($inc = 0; $inc < $level; $inc++) {
$compile = 'Comment*/';
}
return $compile;
}
和许多变化,但似乎都失败了。谢谢。