Example usage for java.beans XMLEncoder writeObject

List of usage examples for java.beans XMLEncoder writeObject

Introduction

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

Prototype

public void writeObject(Object o) 

Source Link

Document

Write an XML representation of the specified object to the output.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    JFrame x = new JFrame("Look at me");
    x.setSize(200, 300);//from w  w  w.  j  a  v  a 2  s.c  o  m
    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);/* www .j a v a2s  . 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/*from   ww  w.  java  2 s  . com*/
    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[] 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);
    encoder.close();/*from  ww  w .j a va  2 s.co  m*/
}

From source file:Main.java

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

    MyClass o = new MyClass();
    o.setProp(1);//from w  w w  .ja va 2s.  co 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[] 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  ww  w.  ja v a2 s. c om*/
    }
    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: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);
    encoder.close();//w w  w  .  jav  a 2  s .c om

}

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);
    e.close();/*  ww w .j  ava 2  s . co  m*/
    return baos.toString();
}

From source file:Main.java

public static boolean serializeObject(File file, Object o) {
    if (file.exists()) {
        System.out.println("Warning! Object Already Exists \n Replacing File.");
    }//from  w  w  w . j av a2s.c  o m
    try {
        FileOutputStream os = new FileOutputStream(file);
        XMLEncoder encoder = new XMLEncoder(os);
        encoder.writeObject(o);
        encoder.close();
    } catch (Exception f) {
        System.out.println("Warning! Write was Unsuccessful");
        return false;
    }
    return true;
}

From source file:Main.java

/***
 * Serialize an object into xml string.//  w w  w .  j  a v  a  2  s  .c  om
 * 
 * @param obj
 *            object to serialize
 * @return Xml string represents the object
 */
public static String serializeObject(final Object obj) {
    final OutputStream bufferStream = new ByteArrayOutputStream();

    // Create XML encoder.
    final XMLEncoder xenc = new XMLEncoder(bufferStream);

    xenc.writeObject(obj);
    // Need to close the XMLEncoder to flush.
    xenc.close();

    final String serializedString = bufferStream.toString();

    // JavaDoc indicates that we will not need to close
    // a ByteArrayOutputStream. From JavaDoc:
    // Closing a ByteArrayOutputStream has no effect. The methods
    // in this class can be called after the stream has been closed
    // without generating an IOException.
    //
    // bufferStream.close();

    return serializedString;
}