I have projectDetails node in my xml document, which is created from java jdom api, and the data inside nodes comes from database.
Problem is with description field which is stored as html inside database.And when I am adding this into <descriptionDetails />
element, and transforming it using Java's transform class it is escaping all the html tags.
Is it possible to get Html code like rest of the tags as child of descriptionDetails and unescaped.
<projectDetails label="label.projectDetails">
<descriptionDetails label="label.descriptionDetails">
<html>
<head></head>
<body>
<strong><strong> Tiny MCE Bold<br /><em>Tiny MCE Bold/Itellic</em><br /><span style="text-decoration: underline;"><em>Tiny MCE Bold/Itellic/Underlined</em></span><br /></strong></strong>
<div>
Lorem Ipsum&nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
<br />
<br />
<span style="color: #ff0000;">printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <span style="color: #ffffff; background-color: #808000;"><span style="background-color: #808000;">sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum,.</span><br /></span></span>
</div>
<h1>H1 heading</h1>
<h2>H1 heading</h2>
<h3>H1 heading</h3>
<h4>H1 heading</h4>
<h5>H1 heading</h5>
<h6>H1 heading<br /><br /><span style="font-size: 14pt;">font size 14</span></h6>
</body>
</html>
</descriptionDetails>
</projectDetails
private static String xmlAsString(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
return out.toString();
}
Expected output,
<projectDetails label="label.projectDetails">
<descriptionDetails label="label.descriptionDetails">
<html>
<head></head>
<body>
<strong><strong> Tiny MCE Bold<br /><em>Tiny MCE Bold/Itellic</em><br /><span style="text-decoration: underline;"><em>Tiny MCE Bold/Itellic/Underlined</em></span><br /></strong></strong>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
<br />
<br />
<span style="color: #ff0000;">printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset <span style="color: #ffffff; background-color: #808000;"><span style="background-color: #808000;">sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum,.</span><br /></span></span>
</div>
<h1>H1 heading</h1>
<h2>H1 heading</h2>
<h3>H1 heading</h3>
<h4>H1 heading</h4>
<h5>H1 heading</h5>
<h6>H1 heading<br /><br /><span style="font-size: 14pt;">font size 14</span></h6>
</body>
</html>
</descriptionDetails>
</projectDetails
Instead of using the default Transformer with newInstance().newTransformer()
you could create with https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/TransformerFactory.html#newTransformer-javax.xml.transform.Source- one from a stylesheet like
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="descriptionDetails/text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
used as the Source.
https://xsltfiddle.liberty-development.net/nc4NzR7
Note however that your escaped HTML contains for instance an entity reference
which would then turn your output into something that is not well-formed XML, as that entity is not predefined in XML.