| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package xin.glue.ui.common;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.util.zip.GZIPOutputStream;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- public class XmlOutput {
- private static Document document;
- public static final int BUFFER = 1024;
- public static final String EXT = ".gz";
- public static void init() {
- try {
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.newDocument();
- } catch (ParserConfigurationException e) {
- System.out.println(e.getMessage());
- }
- }
- public static void createXml(String fileName, String jsonStr,
- String[] colRef, String[] title) {
- init();
- Element root = document.createElement("rows"); // 创建根节点
- document.appendChild(root);
- /* 创建子节点,start */
- Element item;
- Element ele;
- String[] colRefs = colRef[0].split("\\^");
- String[] ts = title[0].split("\\|");
- String[] titles = ts[ts.length-1].split("\\^");
- JSONArray array = JSONArray.parseArray(jsonStr);
- for (int i = 0; i < array.size(); i++) {
- item = document.createElement("row");
- item.setAttribute("index", String.valueOf(i + 1));
- JSONObject jobj = (JSONObject) array.get(i);
- for (int j = 0; j < colRefs.length; j++) {
- ele = document.createElement(colRefs[j]);
- ele.setAttribute("desc", titles[j]);
- ele.setTextContent(jobj.getString(colRefs[j]));
- item.appendChild(ele);
- }
- root.appendChild(item);
- }
- /* end */
- // 将DOM对象document写入到xml文件中
- TransformerFactory tf = TransformerFactory.newInstance();
- try {
- Transformer transformer = tf.newTransformer();
- DOMSource source = new DOMSource(document);
- transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName),"utf-8"));
- StreamResult result = new StreamResult(pw);
- transformer.transform(source, result); // 关键转换
- pw.close();
- // System.out.println("生成XML文件成功!");
- } catch (TransformerConfigurationException e) {
- System.out.println(e.getMessage());
- } catch (IllegalArgumentException e) {
- System.out.println(e.getMessage());
- } catch (FileNotFoundException e) {
- System.out.println(e.getMessage());
- } catch (TransformerException e) {
- System.out.println(e.getMessage());
- } catch (UnsupportedEncodingException e) {
- System.out.println(e.getMessage());
- }
- }
- /**
- * 数据压缩
- *
- * @param data
- * @return
- * @throws Exception
- */
- public static byte[] compress(byte[] data) throws Exception {
- ByteArrayInputStream bais = new ByteArrayInputStream(data);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- // 压缩
- compress(bais, baos);
- byte[] output = baos.toByteArray();
- baos.flush();
- baos.close();
- bais.close();
- return output;
- }
- /**
- * 数据压缩
- *
- * @param is
- * @param os
- * @throws Exception
- */
- public static void compress(InputStream is, OutputStream os)
- throws Exception {
- GZIPOutputStream gos = new GZIPOutputStream(os);
- int count;
- byte data[] = new byte[BUFFER];
- while ((count = is.read(data, 0, BUFFER)) != -1) {
- gos.write(data, 0, count);
- }
- gos.finish();
- gos.flush();
- gos.close();
- }
- /**
- * 文件压缩
- *
- * @param path
- * @throws Exception
- */
- public static void compress(String path) throws Exception {
- compress(path, true);
- }
- /**
- * 文件压缩
- *
- * @param path
- * @param delete
- * 是否删除原始文件
- * @throws Exception
- */
- public static void compress(String path, boolean delete) throws Exception {
- File file = new File(path);
- compress(file, delete);
- }
- /**
- * 文件压缩
- *
- * @param file
- * @param delete
- * 是否删除原始文件
- * @throws Exception
- */
- public static void compress(File file, boolean delete) throws Exception {
- FileInputStream fis = new FileInputStream(file);
- FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);
- compress(fis, fos);
- fis.close();
- fos.flush();
- fos.close();
- if (delete) {
- file.delete();
- }
- }
- }
|