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

/**
 * Serializes an object into an xml file
 * /*from   w  ww.ja va  2 s.  c  om*/
 * @param object object to serialize
 * @param fileName path to file
 */
public static void encodeToFile(Object object, String fileName) throws FileNotFoundException, IOException {
    XMLEncoder encoder = new XMLEncoder(new FileOutputStream(fileName));
    try {
        encoder.writeObject(object);
        encoder.flush();
    } finally {
        encoder.close();
    }
}

From source file:Main.java

public static String parseObj2Xml(Object object) throws Exception {
    XMLEncoder encoder = null;//from   ww  w.  j av  a 2s  .  c o  m
    String str = null;
    BufferedOutputStream bufOut = null;
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        bufOut = new BufferedOutputStream(out);
        encoder = new XMLEncoder(bufOut);
        encoder.writeObject(object);
        encoder.close();
        str = new String(out.toByteArray());
    } catch (Exception ex) {
        throw ex;
    } finally {
        if (out != null)
            out.close();
        if (bufOut != null)
            bufOut.close();
    }
    return str;
}

From source file:Main.java

/***
 * Serialize an object into xml string./* w w  w  . j a  v  a 2  s  .com*/
 * 
 * @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;
}

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.");
    }/*  ww w.j a  va 2 s.  co 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

public static void serializeObjectToFile(final String filePath, final Serializable objectToSeralize)
        throws FileNotFoundException, IOException {

    try (OutputStream file = new FileOutputStream(filePath);
            OutputStream buffer = new BufferedOutputStream(file);
            XMLEncoder output = new XMLEncoder(buffer);) {
        output.writeObject(objectToSeralize);
    }/*from   w w  w  . java2 s.c o  m*/
}

From source file:Main.java

/**
 * Creates from the given Object an xml string.
 * //from   www  . j a v  a  2s . c o m
 * @param <T>
 *            the generic type of the return type
 * @param obj
 *            the obj to transform to an xml string.
 * @return the xml string
 */
public static <T> String toXmlWithXMLEncoder(final T obj) {
    XMLEncoder enc = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        enc.close();
        enc = null;
    } finally {
        if (enc != null) {
            enc.close();
        }
    }
    return baos.toString();
}

From source file:Main.java

public static byte[] encodeObject(Object object, final boolean noisy) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(outputStream);
    encoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception ex) {
            if (noisy)
                ex.printStackTrace();/*from w ww  . jav a2 s  .c o  m*/
        }
    });

    encoder.writeObject(object);
    encoder.close();

    if (noisy)
        System.out.println(outputStream.toString());

    return outputStream.toByteArray();
}

From source file:opdracht2.FileTabellen.java

public static void createEmptyXMLArtikelTabel(String filename) {

    File file = new File(filename);

    try (XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)));) {
        HashMap<Integer, ArtikelPOJO> catalogus = new HashMap<>();
        encoder.writeObject(catalogus);//  w w w  . ja  v a  2  s  .c o  m
    } catch (FileNotFoundException ex) {
        LOGGER.error("file om catalogus in te zetten ontbreekt " + ex);
    }
}

From source file:Main.java

/**
 * Writes an object to an XML file.//  w  w  w.j ava2 s  .c o m
 * 
 * @param bean the bean
 * @param filename the filename
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeBeanToFile(Object bean, String filename) throws IOException {
    XMLEncoder encoder = null;
    try {
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
        encoder.writeObject(bean);
    } finally {
        if (encoder != null) {
            encoder.close();
        }
    }
}

From source file:net.sourceforge.fenixedu.util.tests.ResponseExternalization.java

public static String externalize(Response source) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(out);
    encoder.writeObject(source);/* w  ww .  ja  v a 2 s.com*/
    encoder.close();
    try {
        // I think that this is wrong and that we should get the
        // bytes of the ByteArrayOutputStream interpreted as
        // UTF-8, which is what the XMLEncoder produces in the
        // first place.
        // WARNING: If this is changed, the internalize method
        // should be changed accordingly.
        return out.toString(Charset.defaultCharset().name());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage(), e);
        return out.toString();
    }
}