Html inside XML. Should I use CDATA or encode the HTML [closed]

The two options are almost exactly the same. Here are your two choices: <html>This is &lt;b&gt;bold&lt;/b&gt;</html> <html><![CDATA[This is <b>bold</b>]]></html> In both cases, you have to check your string for special characters to be escaped. Lots of people pretend that CDATA strings don’t need any escaping, but as you point out, you have to make sure … Read more

How to generate CDATA block using JAXB?

Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. If you are using MOXy as your JAXB provider then you can leverage the @XmlCDATA extension: package blog.cdata; import javax.xml.bind.annotation.XmlRootElement; import org.eclipse.persistence.oxm.annotations.XmlCDATA; @XmlRootElement(name=”c”) public class Customer { private String bio; @XmlCDATA public void setBio(String bio) { this.bio = bio; … Read more

How to write CDATA using SimpleXmlElement?

Got it! I adapted the code from this great solution (archived version): <?php // http://coffeerings.posterous.com/php-simplexml-and-cdata class SimpleXMLExtended extends SimpleXMLElement { public function addCData( $cdata_text ) { $node = dom_import_simplexml( $this ); $no = $node->ownerDocument; $node->appendChild( $no->createCDATASection( $cdata_text ) ); } } $xmlFile=”config.xml”; // instead of $xml = new SimpleXMLElement( ‘<site/>’ ); $xml = new SimpleXMLExtended( … Read more

Should I use in HTML5?

The CDATA structure isn’t really for HTML at all, it’s for XML. People sometimes use them in XHTML inside script tags because it removes the need for them to escape <, > and & characters. It’s unnecessary in HTML though, since script tags in HTML are already parsed like CDATA sections. Edit: This is where … Read more

PHP: How to handle

You’re probably not accessing it correctly. You can output it directly or cast it as a string. (in this example, the casting is superfluous, as echo automatically does it anyway) $content = simplexml_load_string( ‘<content><![CDATA[Hello, world!]]></content>’ ); echo (string) $content; // or with parent element: $foo = simplexml_load_string( ‘<foo><content><![CDATA[Hello, world!]]></content></foo>’ ); echo (string) $foo->content; You might … Read more