views:

442

answers:

2

I am using dom->createTextNode() in PHP. I see that it automatically escapes characters e.g \/"" etc. According to the PHP's manual this is a standard behavior.

Is it possible that it doesn't escape any characters?

Thanks.

+1  A: 

If some characters are not escaped, you might not get a valid XML file, in the end.

If you don't want any character to be escaped, maybe using DOMDocument::createCDATASection, to get CDATA sections in your XML file, could help.

Though, note that you will get that kind of things (well, CDATA sections) in your XML :

<tag><![CDATA[<greeting>Hello, world!</greeting>]]></tag>
Pascal MARTIN
A: 

If you can avoid the escape sequences by creating a DomDocumentFragment node and appending raw text with the appendXML() method:

    $rawXMLNode = $domDoc->createDocumentFragment();
    $rawXMLNode->appendXML("<tag>text</tag><tag2>text2</tag2>");
    $someNode->appendChild($rawXMLNode);

However I don't think you can necessarily access the DOM of this raw text without reloading the document. Also some characters are still not allowed in. I needed to do some string processing to insert some php into an html document using this method:

    $elem = $domHtml->getElementById('header');
    $newElem = $domHtml->createDocumentFragment();
    $newElem->appendXML('<div id="header"><?php include("templates/header.html"); QQQ?></div>');
    $elem->parentNode->replaceChild($newElem, $elem);
    $filedata = $domHtml->saveHTML();
    $filedata = str_replace('QQQ', '?', $filedata);
Jacob Dalton