user269867
user269867

Reputation: 3972

DOM Parser- CDATA Query

$dom = new DOMDocument('1.0');
$Rootelement = $dom->createElement('Domain', 'Root element');
$dom->appendChild($Rootelement);

$extraInfo=$dom->createElement('extrainfo');
$Rootelement->appendChild($extraInfo);

$rootTextNode=$dom->createTextNode("Co > S&S ");
$extraInfo->appendChild($rootTextNode);

header ("Content-Type:text/xml"); 
echo $dom->saveXML();

I was trying an example for CDATA Section in DOM and got stuck here. I have created a '' xml node which contains illegal characters like '>' ,'&' but when I see the output of above code browsers do not throw an error saying usage of illegal characters or XML is not well formed.... I remember that this error use to pop up if you do not keep these characters in CDATA Section.

Can someone help me what I am missing.. As per me it should give me some error in browser but I am receiving correct output !!!

Regards, Priti

Upvotes: 0

Views: 489

Answers (1)

Quentin
Quentin

Reputation: 944438

You are programatically constructing a DOM and then converting it to XML.

The library you are using will either wrap the text node in a CDATA section, or represent the characters with special meaning in XML with entities (> etc). Either is valid and equivalent.

This is also why we use XML libraries instead of templates to build XML documents. They take care of all of that for us and stop us ending up with non-well-formed documents.

Upvotes: 2

Related Questions