Example usage for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT

List of usage examples for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT.

Prototype

String JAXB_FORMATTED_OUTPUT

To view the source code for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT.

Click Source Link

Document

The name of the property used to specify whether or not the marshalled XML data is formatted with linefeeds and indentation.

Usage

From source file:Main.java

/**
 * Marshal a JAXB element to a XML DOM document
 *
 * @param jaxbElement//from w ww.ja v  a  2 s.c o m
 *            The root of content tree to be marshalled
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document doMarshallingJAXBObject(Object jaxbElement) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    Document doc = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
        marshaller.marshal(jaxbElement, doc);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return doc;
}

From source file:Main.java

public static final String obj2xml(final Object obj, final boolean formatted) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext contextOut = JAXBContext.newInstance(obj.getClass());
    Marshaller marshallerOut = contextOut.createMarshaller();
    marshallerOut.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    marshallerOut.marshal(obj, writer);//from w  w  w.  j  a  v  a  2  s.co  m

    return new String(writer.getBuffer());
}

From source file:Main.java

/**
 * Saves the data in the file in xml format.
 *
 * @param file Points to a valid xml file containing data that match the {@code classToConvert}.
 *             Cannot be null./*  w w  w  .j  a v  a2s  .com*/
 * @throws FileNotFoundException Thrown if the file is missing.
 * @throws JAXBException         Thrown if there is an error during converting the data
 *                               into xml and writing to the file.
 */
public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException {

    assert file != null;
    assert data != null;

    if (!file.exists()) {
        throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
    }

    JAXBContext context = JAXBContext.newInstance(data.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    m.marshal(data, file);
}

From source file:Main.java

public static <T extends Object> String marshalAsString(Class clz, T marshalObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter writer = new StringWriter();
    marshaller.marshal(marshalObj, new BufferedWriter(writer));
    return writer.toString();

}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * /*  w ww.j a v  a 2 s  .c  o  m*/
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Convert a bean to XML format using JAXB.
 * //from   w  ww.  java 2 s.c  o  m
 * @param bean
 *            The bean to marshal as XML.
 * @param bc
 *            Additional classes to register on the JAXB context for
 *            marshalling.
 * @return An XML representation of the bean.
 */
public static <T> String marshal(T bean, Class<?>... bc) {
    assert bean != null;
    Class<?>[] bind;
    if (bc.length > 0) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = bean.getClass();
        for (int i = 0; i < bc.length; i++)
            bind[i + 1] = bc[i];
    } else
        bind = new Class<?>[] { bean.getClass(), };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(bean, baos);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error marshalling", e);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

public static Document deserialize(Object object) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w ww  .j  av  a  2  s  .c om*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, doc);
    return doc;
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML file//  w  ww.j a  v a 2  s.com
 *
 * @param jaxbElement
 *            The root of content tree to be marshalled
 * @param strXMLFilePath
 *            XML output file path
 * @throws Exception
 *             in error case
 */
public static void doMarshalling(Object jaxbElement, String strXMLFilePath) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    if (strXMLFilePath == null) {
        throw new RuntimeException("No XML file path (null)!");
    }
    FileOutputStream fos = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        fos = new FileOutputStream(strXMLFilePath);
        marshaller.marshal(jaxbElement, fos);// System.out);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (fos != null) {
            fos.close();
            fos = null;
        }
    }
}

From source file:Main.java

public static Marshaller createMarshaller(String pack, Schema schema) {
    JAXBContext jaxbContext = null;
    try {/*from   w w  w . j  a  v  a2s .  c  om*/
        jaxbContext = JAXBContext.newInstance(pack);
        Marshaller marsh = jaxbContext.createMarshaller();

        if (schema != null) {
            marsh.setSchema(schema);
            //                marsh.setEventHandler( new DefaultValidationEventHandler() {
            //                    @Override
            //                    public boolean handleEvent( ValidationEvent event ) {
            //                        return super.handleEvent( event );
            //                    }
            //                });
        }
        marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        return marsh;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Prints the document for debug./*from  ww w  . ja  v  a  2s . co m*/
 *
 * @param model the model to be printed
 * @throws Exception for any errors encountered
 */
public static void printModel(Object model) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance("gov.medicaid.domain.model");
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(model, System.out);
}