fusionchart不規則XML的正則解析及Dom4j解析

java方法

	public static  ArrayList<String> getData(String in ,String name){
		//in = in.replaceAll("\n","");	
		Pattern p = null;
		try {
	p = Pattern.compile(""+name+"\\s*=\\s*\\'\\w*\\S*\\s*\\'");//正則解析		
		} catch (Exception e) {
		e.printStackTrace();
		}
	Matcher m = p.matcher(in); 
	ArrayList<String> list = new ArrayList<String>();	//用List方便一些
		while(m.find()){      
		String s[] = m.group().split("'");	
		 if(s.length<=1){
		    	s = " ' ' ".split("'"); //處理沒有數據的情況 避免數組越界
		     }
			list.add(s[1]);
		}
		return list;	

	}                                                                                                                         
然後是導出數據到Excel的方法(寫Excel用的是jxl JAR包)
public static  boolean expoertExcel(String res){//res便是傳遞給fusionchart的數據 如果是文件形式 可以通過輸入流讀取
 ArrayList<String> capt = new ArrayList<String>();   
 ArrayList<String> seri = new ArrayList<String>();
 ArrayList<String> setv = new ArrayList<String>();
 ArrayList<String> name = new ArrayList<String>();
 ArrayList<String> ysname = new ArrayList<String>();
  capt = getData(res,"caption");
  seri = getData(res,"seriesName");
  setv = getData(res, "value");  
  name = getData(res, "name");
 ArrayList<String> sw = getData(res, "xAxisName"); 
   String xname = sw.get(0);
 ysname = getData(res, "yAxisName");
 // jxl寫 Excel
 File ex =  new File("C:\\test.xls");//web項目的路徑亂糟糟 運行的時候跑到tomcat的bin目錄去了 先這樣搞一下
         if(ex.exists()){
            ex.delete();
                        }
             try {
                 if(!ex.exists()){
                 ex.createNewFile();
                                 } 
               } catch (Exception e) {
                 e.printStackTrace();
               }          
        WritableWorkbook wk = null; 
        try {
  wk = Workbook.createWorkbook(ex);
  } catch (IOException e) {
  e.printStackTrace();
  }
  WritableSheet sheet = null; 
  Label label = null;
  Number num=null;
    sheet = wk.createSheet(capt.get(0),0);
    label = new Label(0,0,capt.get(0));
     try {
    sheet.addCell(label);
   } catch (RowsExceededException e1) {
    e1.printStackTrace();
   } catch (WriteException e1) {
    e1.printStackTrace();
   }
 int snum = seri.size();  
 int lnum = name.size();
 int vnum = setv.size();
  int[][] p = new int[lnum][vnum];
 for(int i=0;i<snum;i++){
  label = new Label(0,i,seri.get(i));
  if(sheet != null){
  try {
   sheet.addCell(label);
  } catch (Exception e) {
   e.printStackTrace();
  } 
 }
  for(int j=i*lnum;j<(i+1)*lnum;j++){
   num = new Number(j-i*lnum+1,i,Double.parseDouble(setv.get(j)));
   try {
    sheet.addCell(num);
   } catch (RowsExceededException e) {
    e.printStackTrace();
   } catch (WriteException e) {
    e.printStackTrace();
   }
  }
 }
  for(int i=0;i<lnum;i++){
   label = new Label(i+1,snum,name.get(i)+xname);
   try {
    sheet.addCell(label);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 label = new Label(0,snum+1,"數據單位是:"+ysname.get(0));
  try {
   sheet.addCell(label);
  } catch (Exception e) {
   e.printStackTrace();
  }
 try {
  wk.write();
 } catch (IOException e) {
  e.printStackTrace();
 }
 try {
  wk.close();
 } catch (Exception e) {
  e.printStackTrace();
 }
 if(ex.exists()){
  System.out.println(ex.getPath());
  System.out.println(ex.toURI());
  return true;
 }else{
  return false; 
 }
 }//暫時還沒想到好的思路 這算是一種笨方法吧 繼續積累知識,找到更好的解決方法
 搗鼓了幾天的Dom4j 是可以解析fusionchart的xml文件的 代碼如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

public class TestDom4j {

	/**
	 * @param args
	 * author txz  
	 */
	public static void main(String[] args) {
		test();
	}
	
	public static void test(){
	File file = new File("G:\\xml\\line.xml");
		if(!file.exists()){
			System.out.println("文件不存在");
			return;
		}
		InputStream in = null;
		try {
			in = new FileInputStream(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		SAXReader read = new SAXReader();
		Document document = null;
		try {
			document = read.read(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String caption="";
		List list = document.selectNodes("//chart/@caption");//獲取caption屬性值
			Iterator it = list.iterator();
			while (it.hasNext()) {
			Attribute cap = (Attribute)it.next();
				caption = cap.getValue();
			}
		System.out.println(caption);
		ArrayList<String> labelarList = new ArrayList<String>();
		List labellist = document.selectNodes("//category/@label");//獲取lable屬性值
			Iterator itl = labellist.iterator();
			while (itl.hasNext()) {
			Attribute labelAttribute = (Attribute)itl.next();
				labelarList.add(labelAttribute.getValue());
			}
		for(int i=0;i<labelarList.size();i++){
			System.out.println(labelarList.get(i));//測試一下
		}
		ArrayList<String> dataArrayList = new ArrayList<String>();
		List dataList = document.selectNodes("//dataset/@seriesName");//獲取seriesName屬性的值
			Iterator itd = dataList.iterator();
			while (itd.hasNext()) {
			Attribute dataAttribute = (Attribute)itd.next();
			dataArrayList.add(dataAttribute.getValue());
			}
			for(int i=0;i<dataArrayList.size();i++){
				System.out.println(dataArrayList.get(i));//測試一下
			}		
		ArrayList<String> valueArrayList = new ArrayList<String>();
		List valueList = document.selectNodes("//set/@value");//獲取value屬性的值
			Iterator itv = valueList.iterator();
			while (itv.hasNext()) {
			Attribute valueAttribute = (Attribute)itv.next();
			valueArrayList.add(valueAttribute.getValue());
			}
			for(int i=0;i<valueArrayList.size();i++){
				System.out.println(valueArrayList.get(i));//測試一下
			}
	//需要的數據都取到了 下邊就可以寫到Excel裏了
			File xls = new File("G:\\test.xls");
				if(xls.exists()){
					xls.delete();
				}
				try {
				xls.createNewFile();
				WritableWorkbook work = Workbook.createWorkbook(xls);
				WritableSheet sheet = work.createSheet(caption,2);
		int x=0,y=0,z=0;		
			x = dataArrayList.size();
			y = labelarList.size();
			z = valueArrayList.size();
			System.out.println(x+" "+y+" "+z);
		for(int i=0;i<x;i++){
		Label data = new Label(0,i,dataArrayList.get(i));	
			sheet.addCell(data);
			for(int j=0;j<y;j++){
		Double dou = Double.parseDouble(valueArrayList.get(i*y+j));// 		
			Number val = new Number(j+1,i,dou);	
				sheet.addCell(val);
			}
		}
		for(int i=0;i<y;i++){
			Label label = new Label(i+1,x,labelarList.get(i));
			sheet.addCell(label);
		}		
		work.write();
		work.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
下邊是解析的fusionchart的xml文件
<chart caption='Daily Visits' subcaption='(from 8/6/2006 to 8/12/2006)' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2'   divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='5' alternateHGridColor='CC3300' shadowAlpha='40' labelStep="2" numvdivlines='5' chartRightMargin="35" bgColor='FFFFFF,CC3300' bgAngle='270' bgAlpha='10,10'>
<categories >
<category label='8/6/2006' />
<category label='8/7/2006' />
<category label='8/8/2006' />
<category label='8/9/2006' />
<category label='8/10/2006' />
<category label='8/11/2006' />
<category label='8/12/2006' />

</categories>
<dataset seriesName='Offline Marketing' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>
	<set value='1327' />
	<set value='1826' />
	<set value='1699' />
	<set value='1511' />
	<set value='1904' />
	<set value='1957' />
	<set value='1296' />
	</dataset>

<dataset seriesName='Search' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>
	<set value='2042' />
	<set value='3210' />
	<set value='2994' />
	<set value='3115' />
	<set value='2844' />
	<set value='3576' />
	<set value='1862' />
</dataset>

<dataset seriesName='Paid Search' color='2AD62A' anchorBorderColor='2AD62A' anchorBgColor='2AD62A'>
	<set value='850' />
	<set value='1010' />
	<set value='1116' />
	<set value='1234' />
	<set value='1210' />
	<set value='1054' />
	<set value='802' />
</dataset>

<dataset seriesName='From Mail' color='DBDC25' anchorBorderColor='DBDC25' anchorBgColor='DBDC25'>
	<set value='541' />
	<set value='781' />
	<set value='920' />
	<set value='754' />
	<set value='840' />
	<set value='893' />
	<set value='451' />
</dataset>
	<styles>                
		<definition>
                         
			<style name='CaptionFont' type='font' size='12'/>
		</definition>
		<application>
			<apply toObject='CAPTION' styles='CaptionFont' />
			<apply toObject='SUBCAPTION' styles='CaptionFont' />
		</application>
	</styles>

</chart>

需要說幾句的是最開始學習dom4j寫練習的時候一直在報錯 費了些勁 原來簡單的dom4j可不是就這一個jar包就能搞定的還要有 xerces.jar,jaxen-1.1.1.jar 後邊寫Excel又加上了 jxl.jar

不然就會報各種各樣的錯誤。 

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章