Sangram Anand
Sangram Anand

Reputation: 10844

Printing multiple tags within CDATA section

I have a old xml file which was generated manually in java. Its tree structure is like this.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tvshows.xsl"?>
<rss version='0.91'>
<channel>
<title>xyz.com</title>
<link>http://www.xyz.com</link>
<description></description>
<item>
<title>Downton Abbey</title>
<link>http://www.xyz.com</link>
<description><![CDATA[
<tr class='chartContent'>
<td class='rank'>1.</td>
<td class='showTitle'>Dexter</td>
<td class='network'>CBS</td>
<td class='sumInvIndex'>210</td>
<td class='earlierWeek'>-13</td>
<td class='mediaInvIndex'>225</td>
<td class='socialNetworkInvIndex'>238</td>
<td class='gammaIndex'>--</td>
</tr>]]>
</description>
</item>
</channel>
</rss>

Right now am using the JDOM library to generate the exact format. But how should i deal with the CDATA[]. I find <tr> with almost 10 columns. I am trying to fix it with the

CDATA cdata = new CDATA("<tr class='chartContent'>");
cdata.append("<td class='rank'>" + current.getRank() + "</td>");
cdata.append("\n");
cdata.append("<td class='showTitle'>" + current.getShowTitle() + "</td>");
cdata.append("<td class='network'>" + current.getNetwork() + "</td>");
cdata.append("<td class='sumInvIndex'>" + current.getsumInvIndex() +"</td>");
cdata.append("<td class='earlierWeek'>" + current.getearlierWeek() + "</td>");
cdata.append("<td class='mediaInvIndex'>" + current.getmediaInvIndex() + "</td>");
cdata.append("<td class='socialNetworkInvIndex'>" + current.getsocialNetworkInvIndex() + "</td>");
cdata.append("<td class='gammaIndex'>" + current.getgammaIndex() + "</td>");
cdata.append("</tr>");

Element description = new Element("description");  
description.setContent(cdata);

But is there a optimal way of appending tags to columns something like

Element rankTD = new Element("td");
rankTD.setText(current.getRank());
& add rankTD element to cdata .

The generated out put after using

Format format = null;
format = Format.getPrettyFormat();
content.add(new Element("td").setText(current.getRank()).setAttribute("class","showTitle"));
-------------------
-------------------
-------------------
String cdataContent = new XMLOutputter(format).outputString(content);

Output:

<description><![CDATA[<tr class="chartContent" />
<td class="showTitle">1.</td>
<td class="network">PBS</td>
<td class="sumInvIndex">210</td>
<td class="earlierWeek">-13</td>
<td class="mediaInvIndex">225</td>
<td class="socialNetworkInvIndex">238</td>
<td class="gammaIndex">--</td>]]></description>

Upvotes: 0

Views: 443

Answers (1)

axtavt
axtavt

Reputation: 242686

You need to encode content of the CDATA section to String separately and put that String into CDATA, something like this:

List<Element> content = new ArrayList<Element>();
content.add(new Element("td").setText(current.getRank());
...
String cdataContent = new XMLOutputter().outputString(content);
description.setContent(new CDATA(cdataContent));

Upvotes: 1

Related Questions