PHP : Function Reference : DOM Functions : DOMDocument->createTextNode()
Examples ( Source code ) » DOMDocument->createTextNode()
<?php
$doc = new DOMDocument('1.0');
$root = $doc->createElement('html');
$root = $doc->appendChild($root);
$head = $doc->createElement('head');
$head = $root->appendChild($head);
$title = $doc->createElement('title');
$title = $head->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo $doc->saveHTML();
?>
carltond
I wanted to add a raw XML string to the XML I was creating but had problems using DOMDocument->createTextNode() as it encoded my tags.
Have a look at DOMDocument->createDocumentFragment() and the the "appendXML" method...an example from the page
<?php
$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();
?>
Will output...
<?xml version="1.0" ?>
<root><foo>text</foo><bar>text2</bar></root>
jon
About escaping characters:
This is standard behavior. brackets, ampersans, and maybe even quoted will be "escaped" if you output your XML as a string.
This should not, however, happen if you append your text node to a CDATA section.
|