|
|
|
@ -34,32 +34,31 @@ public class TestXml {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从XML中获取图表数据
|
|
|
|
|
*
|
|
|
|
|
* @param xmlPath
|
|
|
|
|
* @return
|
|
|
|
|
* @throws DocumentException
|
|
|
|
|
*/
|
|
|
|
|
public static List<List<String>> getChartData(String xmlPath) throws DocumentException {
|
|
|
|
|
// 创建 SAXReader 对象,读取 XML 文件
|
|
|
|
|
SAXReader reader = new SAXReader();
|
|
|
|
|
Document document = reader.read(new File(xmlPath));
|
|
|
|
|
// 获取根元素
|
|
|
|
|
Element root = document.getRootElement();
|
|
|
|
|
|
|
|
|
|
//声明一个数组,图表的所有类型
|
|
|
|
|
//图表类型
|
|
|
|
|
String[] CHART_TYPES = {"lineChart", "barChart"};//折线,柱状
|
|
|
|
|
|
|
|
|
|
SAXReader reader = new SAXReader(); // 创建 SAXReader 对象,读取 XML 文件
|
|
|
|
|
Document document = reader.read(new File(xmlPath));
|
|
|
|
|
Element root = document.getRootElement();// 获取根元素
|
|
|
|
|
|
|
|
|
|
List<List<String>> tList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Element> children = root.element("chart").element("plotArea").elements();//工作区
|
|
|
|
|
List<Element> xList = new ArrayList<>();
|
|
|
|
|
boolean hadReadXList = false;//是不是已经读取过横坐标也就是年份的数据了
|
|
|
|
|
for (Element child : children) {
|
|
|
|
|
//将CHART_TYPES数组转换为List
|
|
|
|
|
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);
|
|
|
|
|
List<String> CHART_TYPES_LIST = Arrays.asList(CHART_TYPES);//将CHART_TYPES数组转换为List
|
|
|
|
|
if (CHART_TYPES_LIST.contains(child.getName())) {//如果当前遍历到的子元素是折线图或者柱状图
|
|
|
|
|
//System.out.println("找到图表类型:" + child.getName());
|
|
|
|
|
String type = child.getName();
|
|
|
|
|
//ce:chart element
|
|
|
|
|
Element ce = root.element("chart").element("plotArea").element(type);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ce.elements("ser").size(); i++) {
|
|
|
|
|
Element ser = ce.elements("ser").get(i);
|
|
|
|
|
//cat 标签
|
|
|
|
@ -67,10 +66,9 @@ public class TestXml {
|
|
|
|
|
Element numRef = cat.element("numRef");
|
|
|
|
|
//数据
|
|
|
|
|
Element numCache = numRef.element("numCache");
|
|
|
|
|
//多个数据组,xList一定是一样的,并且它是最全的
|
|
|
|
|
if (xList.isEmpty()) {
|
|
|
|
|
xList = numCache.elements("pt");
|
|
|
|
|
//构造横向的年份列表
|
|
|
|
|
//多个数据组,xList(横坐标年份数据)一定是一样的,并且它是最全的,只读取一次即可
|
|
|
|
|
if (!hadReadXList) {
|
|
|
|
|
List<Element> xList = numCache.elements("pt");
|
|
|
|
|
List<String> yearList = new ArrayList<>();
|
|
|
|
|
for (Element pt : xList) {
|
|
|
|
|
String v = pt.element("v").getText();
|
|
|
|
@ -78,31 +76,29 @@ public class TestXml {
|
|
|
|
|
}
|
|
|
|
|
//添加到横向的表中
|
|
|
|
|
tList.add(yearList);
|
|
|
|
|
hadReadXList = true;//标识读取过了
|
|
|
|
|
}
|
|
|
|
|
//======================================================
|
|
|
|
|
//val标签
|
|
|
|
|
Element val = ser.element("val");
|
|
|
|
|
numRef = val.element("numRef");
|
|
|
|
|
|
|
|
|
|
numCache = numRef.element("numCache");
|
|
|
|
|
List<Element> yList = numCache.elements("pt");
|
|
|
|
|
//将客观存在的索引号保存到HashMap中
|
|
|
|
|
Map<String, String> existsMap = new HashMap<>();
|
|
|
|
|
//不是每个索引号都一定有数据的,所以,先将存在的索引号保存到HashMap中,后面再遍历每个年份的索引号时,有的就读取数值,没有的就写成null
|
|
|
|
|
Map<String, String> _map = new HashMap<>();
|
|
|
|
|
for (Element pt : yList) {
|
|
|
|
|
String idx = pt.attribute("idx").getValue();
|
|
|
|
|
String v = pt.element("v").getText();
|
|
|
|
|
//保留两位小数的字符串
|
|
|
|
|
v = doubleWith2f(v);
|
|
|
|
|
//是不是有效的,存在的数据,因为有的数据是未填写的
|
|
|
|
|
if (!existsMap.containsKey(idx)) {
|
|
|
|
|
existsMap.put(idx, v);
|
|
|
|
|
}
|
|
|
|
|
//保存到map中,一会再遍历时就知道哪个有,哪个没有,没有的需要填充null
|
|
|
|
|
_map.put(idx, v);
|
|
|
|
|
}
|
|
|
|
|
//枚举一遍全的xList
|
|
|
|
|
List<String> vList = new ArrayList<>();
|
|
|
|
|
//首行就是年份行,它的size()就是标准宽度
|
|
|
|
|
for (int j = 0; j < tList.getFirst().size(); j++) {
|
|
|
|
|
vList.add(existsMap.getOrDefault(String.valueOf(j), null));
|
|
|
|
|
vList.add(_map.getOrDefault(String.valueOf(j), null));
|
|
|
|
|
}
|
|
|
|
|
tList.add(vList);
|
|
|
|
|
}
|
|
|
|
|