FusionCharts 动态生成XML数据问题

前端之家收集整理的这篇文章主要介绍了FusionCharts 动态生成XML数据问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前台页面

var chart = new FusionCharts("${ctx}/plugins/FusionCharts/Charts/Column3D.swf","ChartId","904","300");  
chart.setDataURL("${ctx}/info/getDataInfo"); 

后台代码

try {
    String beginYear = super.getParameter("beginYear");
    String endYear = super.getParameter("endYear");
    StringBuffer wsdwFileContent = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><chart caption=\"" + beginYear + "年至" + endYear + "年\" baseFontSize=\"12\" palette=\"2\" bgColor=\"#99CCFF,#FFFFFF\"></chart>");
    getResponse().setContentType("application/xml");
    getResponse().setCharacterEncoding("utf-8");
    byte[] tjkjByte = wsdwFileContent.toString().getBytes("utf-8");
    getResponse().getOutputStream().write(tjkjByte);
} catch (IOException e) {
    e.printStackTrace();
}

最终页面怎么也无法生成图表,显示"Invalid XML Data"。

网上查找资料说是在生成XML文档前,必须插入BOM标记(Byte Order Mark),代码如下:

try {
    String beginYear = super.getParameter("beginYear");
    String endYear = super.getParameter("endYear");
    StringBuffer wsdwFileContent = new StringBuffer(getBom() + "<?xml version=\"1.0\" encoding=\"UTF-8\"?><chart caption=\"" + beginYear + "年至" + endYear + "年\" baseFontSize=\"12\" palette=\"2\" bgColor=\"#99CCFF,#FFFFFF\"></chart>");
    getResponse().setContentType("application/xml");
    getResponse().setCharacterEncoding("utf-8");
    byte[] tjkjByte = wsdwFileContent.toString().getBytes("utf-8");
    getResponse().getOutputStream().write(tjkjByte);
} catch (IOException e) {
    e.printStackTrace();
}

getBom() 方法

public String getBom () {
    String bom = null;
    try {
        byte[] bomByte = new byte[]{
            (byte) 0xef,(byte) 0xbb,(byte) 0xbf
        };
        bom = new String(bomByte,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return bom;
}
原文链接:/xml/299996.html

猜你在找的XML相关文章