XML使用字符串拼接循环拼接问题?
如上图所示,我现在要做一个三维图表:
这是标注XML格式:<chart palette='2' caption='Country Comparison' showLabels='1' showvalues='0' decimals='0' numberPrefix='$' clustered='0' exeTime='1.5' showPlotBorder='0' zGapPlot='30' zDepth='90' divLineEffect='emboss' startAngX='10' endAngX='18' startAngY='-10' endAngY='-40'>
<categories>
<category label='Austria' />
<category label='Brazil' />
<category label='France' />
<category label='Germany' />
<category label='USA' />
</categories>
<dataset seriesName='1998' color='8BBA00' showValues='0'>
<set value='45000.65' />
<set value='44835.76' />
<set value='18722.18' />
<set value='77557.31' />
<set value='92633.68' />
</dataset>
<dataset seriesName='1997' color='F6BD0F' showValues='0'>
<set value='57401.85' />
<set value='41941.19' />
<set value='45263.37' />
<set value='117320.16' />
<set value='114845.27' />
</dataset>
<dataset seriesName='1996' color='AFD8F8' showValues='0'>
<set value='25601.34' />
<set value='20148.82' />
<set value='17372.76' />
<set value='35407.15' />
<set value='38105.68' />
</dataset>
<styles>
<definition>
<style name='captionFont' type='font' size='15' />
</definition>
<application>
<apply toObject='caption' styles='captionfont' />
</application>
</styles>
</chart>
下面是我自己在java类里拼接但是每次到循环哪里都拼接不对,导致数据输出错误,图表无法显示:
public String buildChart(List<YearSale> datas) {
StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("chart palette='2' caption='年份销售' yAxisName='Units' showLabels='1' showvalues='0' decimals='0' numberPrefix='$' clustered='0' exeTime='1.5' showPlotBorder='0' zGapPlot='30' zDepth='90' divLineEffect='emboss' startAngX='10' endAngX='18' startAngY='-10' endAngY='-40'>");
if (null != datas && datas.size() > 0) {
int i = 0;
sb.append("<categories>");
//向label属性里面添加姓名
for (YearSale sale : datas) {
sb.append("<category label='"+sale.getSaleName()+"'/>");
i++;
}
sb.append("</categories>");
//双层循环XXXX年份的销售额
for (YearSale yearSale : datas) {
sb.append("<dataset seriesName='"+yearSale.getSaleYear()+"' showValues='0'>");
i++;
for (int j = 0; j < saleList.size(); j++) {
sb.append("<set value='"+yearSale.getSaleMoney()+"'/>");
}
}
sb.append(" </dataset>");
}
sb.append("<styles><definition> <style name='captionFont' type='font' size='15' /> </definition><application> <apply toObject='caption' styles='captionfont' /> </application></styles></chart>");
System.out.println(sb.toString());
return sb.toString();
}
<dataset seriesName='2011' showValues='0'><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/><set value='32532'/>
<dataset seriesName='2012' showValues='0'><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/><set value='55438'/>
<dataset seriesName='2013' showValues='0'><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/><set value='64321'/>
<dataset seriesName='2011' showValues='0'><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/><set value='44213'/>
<dataset seriesName='2012' showValues='0'><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/><set value='52321'/>
<dataset seriesName='2013' showValues='0'><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/><set value='60341'/> </dataset>
<styles><definition> <style name='captionFont' type='font' size='15' /> </definition><application> <apply toObject='caption' styles='captionfont' /> </application></styles></chart>
for (YearSale yearSale : datas) {
sb.append("<dataset seriesName='"+yearSale.getSaleYear()+"' showValues='0'>");
i++;
for (int j = 0; j < saleList.size(); j++) {
sb.append("<set value='"+yearSale.getSaleMoney()+"'/>"); //这里必然输出时一样的数值
}
//这里没有dataset的结尾
}