quarta-feira, 31 de julho de 2013

Import xml do excel em java com Xstream


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package teste.xml.excel;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;

/**
 *
 * @author smart
 */
public class Teste {

    public static void main(String[] args) throws IOException, Exception {

        Teste t = new Teste();
        XStream xstream = t.getXStream();
        xstream.setMode(XStream.NO_REFERENCES);

        xstream.alias("Workbook", Teste.Workbook.class);
        xstream.addImplicitArray(Teste.Workbook.class, "Worksheet", Teste.Worksheet.class);
        xstream.alias("Table", Teste.Table.class);
        xstream.addImplicitArray(Teste.Table.class, "Row", Teste.Row.class);
        xstream.addImplicitArray(Teste.Row.class, "Cell", Teste.Cell.class);
        xstream.alias("Cell", Teste.Cell.class);
        xstream.alias("Data", String.class);

        Teste.Workbook o = (Teste.Workbook) xstream.fromXML(new File("/home/smart/NetBeansProjects/Teste Rest/build/classes/teste/xml/excel/excel.xml"), new Teste.Workbook());
        System.out.println(o);
    }

    public XStream getXStream() {
        XStream xstream = new XStream() {

            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {

                    @Override
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                        return super.shouldSerializeMember(definedIn, fieldName);
                    }
                };
            }
        };

        return xstream;
    }
 
    private String getURIArquivo(String arquivo) throws Exception {
       URL is = this.getClass().getResource(arquivo);
       if (is == null) {
           is = this.getClass().getClassLoader().getResource(arquivo);
       }
       return URLDecoder.decode(is.toString(), "UTF-8");
   }

    static class Workbook {

        private List<Worksheet> Worksheet;

        public List getWorksheet() {
            return Worksheet;
        }

        public void setWorksheet(List Worksheet) {
            this.Worksheet = Worksheet;
        }
    }

    static class Worksheet {

        private Teste.Table Table;

        public Teste.Table getTable() {
            return Table;
        }

        public void setTable(Teste.Table Table) {
            this.Table = Table;
        }
    }

    static class Table {

        private List<Row> Row;

        public List getRow() {
            return Row;
        }

        public void setRow(List Row) {
            this.Row = Row;
        }
    }

    static class Row {

        private List<Cell> Cell;

        public List getCell() {
            return Cell;
        }

        public void setCell(List Cell) {
            this.Cell = Cell;
        }
    }

    static class Cell {

        private String Data;

        public String getData() {
            return Data;
        }

        public void setData(String Data) {
            this.Data = Data;
        }
    }
}

Nenhum comentário:

Postar um comentário