草庐IT

PHP DOMDocument innerText

coder 2024-04-06 原文

我如何将文本添加到 XML 元素中?例如:

<videoTitle>I want to add text here</videoTitle>

我已经创建了 DOMDocument,并开始添加元素。这是我需要将文本添加到的元素。

$title = $vitals->appendChild($X->createElement("title"));

最佳答案

您需要使用 DOMDocument::createTextNode

$text = $X->createTextNode('Some text here');
$title->appendChild($text);

或者,您可以使用 DOMNode::$nodeValue 的快捷语法:

$title->nodeValue = 'Some text here';

使用此技术时,您必须记住 nodeValue 设置文本内容,而不是 XML 内容。标记被转义,而不是被解析。

关于PHP DOMDocument innerText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6103912/

有关PHP DOMDocument innerText的更多相关文章

随机推荐