Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOMDocument innerText

How would I go about adding text into an XML element? For example:

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

I have created the DOMDocument, and begun adding the elements. Here is the element that I need to add the text to.

$title = $vitals->appendChild($X->createElement("title"));
like image 376
Lee Loftiss Avatar asked Oct 11 '22 23:10

Lee Loftiss


1 Answers

You need to use DOMDocument::createTextNode

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

Alternatively, you can use the shortcut syntax of DOMNode::$nodeValue:

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

You have to remember with this technique that nodeValue sets the text content, not the XML content. Tags are escaped, not parsed.

like image 189
lonesomeday Avatar answered Oct 14 '22 08:10

lonesomeday