Example usage for java.beans XMLEncoder XMLEncoder

List of usage examples for java.beans XMLEncoder XMLEncoder

Introduction

In this page you can find the example usage for java.beans XMLEncoder XMLEncoder.

Prototype

public XMLEncoder(OutputStream out) 

Source Link

Document

Creates a new XML encoder to write out JavaBeans to the stream out using an XML encoding.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    MyClass o = new MyClass(123);

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("outfilename.xml")));

    String[] propertyNames = new String[] { "prop" };
    encoder.setPersistenceDelegate(MyClass.class, new DefaultPersistenceDelegate(propertyNames));

    encoder.writeObject(o);//from ww w  .  jav  a  2 s.c o  m
    encoder.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    MyClass o = new MyClass();
    o.setProp(1);/*from   ww  w .  ja v  a 2  s .c o  m*/
    o.setProps(new int[] { 1, 2, 3 });

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("outfilename.xml")));
    encoder.writeObject(o);
    encoder.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Item bean = new Item();
    bean.setId(new Long(1));
    bean.setItemName("a");
    bean.setItemColour("Red");
    bean.setItemQuantities(new Integer(100));

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("Bean.xml")));

    encoder.writeObject(bean);//from  ww  w. jav a2s. co  m
    encoder.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame x = new JFrame("Look at me");
    x.setSize(200, 300);//from   ww  w.j  a  va 2s . c  om
    x.setVisible(true);
    x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FileOutputStream f;
    try {
        f = new FileOutputStream("Test.xml");
        XMLEncoder e = new XMLEncoder(new BufferedOutputStream(f));
        e.writeObject(x);
        e.close();
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    MyClass o = new MyClass();
    o.setProp(1);//w w w .j  a v a 2  s  .  c  o m
    o.setProps(new int[] { 1, 2, 3 });

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("outfilename.xml")));
    encoder.writeObject(o);
    encoder.close();

}

From source file:MyBean.java

public static void main(String args[]) throws IOException {
    // Create/*w  ww  . j  av a2  s .co  m*/
    MyBean saveme = new MyBean();
    saveme.setNames(new String[] { "one", "two", "three" });
    saveme.setPoint(new Point(15, 27));
    saveme.setLength(6);
    // Save
    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("saveme.xml")));
    encoder.writeObject(saveme);
    encoder.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor propertyDescriptor = pds[i];
        if (propertyDescriptor.getName().equals("itemQuantities")) {
            propertyDescriptor.setValue("transient", Boolean.TRUE);
        }/*from w  w w  . j  a v  a 2  s. c  o m*/
    }
    BeanToXmlTransient bean = new BeanToXmlTransient();
    bean.setId(new Long(1));
    bean.setItemName("Item");
    bean.setItemColour("Red");
    bean.setItemQuantities(new Integer(100));

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("BeanTransient.xml")));

    encoder.writeObject(bean);
    encoder.close();

}

From source file:com.icesoft.applications.faces.address.XWrapperUtil.java

public static void main(String[] args) {

    if (log.isDebugEnabled()) {
        log.debug("Converting database...");
    }// ww w.  j  av a  2 s. c o m

    //load the CSV file
    InputStream is = MatchAddressDB.class.getResourceAsStream(CSV_ADDRESS_DB);
    BufferedReader buff = new BufferedReader(new InputStreamReader(is));

    XMLEncoder xEncode = null;
    XAddressDataWrapper xData;

    //open the XML encoder and attempt to write the xml file
    try {
        xEncode = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(XML_GZ_ADDRESS_DB)));
    } catch (FileNotFoundException ex) {
        log.error("Database could not be written.", ex);
    }

    //first line
    char[] line = getNextLine(buff);

    while (line != null) {

        //get three strings within quotes
        String addressValues[] = new String[3];
        int stringValueStart = 0, stringValueEnd;

        for (int i = 0; i < 3; i++) {
            //opening quote
            while (line[stringValueStart++] != '\"') {
            }
            stringValueEnd = stringValueStart + 1;
            //closing quote
            while (line[stringValueEnd] != '\"') {
                stringValueEnd++;
            }
            //value
            addressValues[i] = new String(line, stringValueStart, stringValueEnd - stringValueStart);
            stringValueStart = stringValueEnd + 1;
        }

        //assign the data to the wrapper
        xData = new XAddressDataWrapper();
        xData.setCity(addressValues[1]);
        xData.setState(addressValues[2]);
        xData.setZip(addressValues[0]);

        //read the next line (entry) in the CSV file
        line = getNextLine(buff);
    }
    //close the XML Encoder
    try {
        xEncode.close();
    } catch (NullPointerException npe) {
        log.error("Could not close XML Encoder.", npe);
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Closed XML Encoder.");
    }
}

From source file:Main.java

public static String toXml(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder e = new XMLEncoder(baos);
    e.writeObject(o);/*  w  ww .ja  v a2 s . c  o  m*/
    e.close();
    return baos.toString();
}

From source file:Main.java

public static void writeBeanToXml(String path, Object bean) {
    XMLEncoder encoder = null;//from   w  ww . jav  a2 s  .co m
    try {
        // Serialize object into XML
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(path)));
        encoder.writeObject(bean);
        encoder.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (encoder != null) {
            encoder.close();
        }
    }
}