5

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"));

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, great, thanks. BTW, is there some way in PHP to automatically add <![CDATA[]]> tags?
@Lee No problem. Have a look at DOMDocument::createCDATASection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.