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

public static <T> void serialize(T object, OutputStream resultStream, String schemaLocation)
        throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(object, resultStream);
}

From source file:com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil.java

/**
 * JsCheckOptionsxml????.//w w  w.j  av  a 2 s .c  o m
 * 
 * 
 * @param checkOptions .
 * @param output .
 */
public static void saveJsCheckOption(JsCheckOption checkOptions, IFile output) {
    try {
        Marshaller marshall = jc.createMarshaller();
        marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));
        StringWriter writer = new StringWriter();
        marshall.marshal(checkOptions, writer);
        if (output.exists()) {
            output.setContents(IOUtils.toInputStream(writer.toString(), "UTF-8"), IResource.FORCE, null);
        } else {
            output.create(IOUtils.toInputStream(writer.toString(), "UTF-8"), true, null);
        }
        output.refreshLocal(IResource.DEPTH_ONE, null);
    } catch (JAXBException e) {
        logger.put(Messages.EM0006, e, output.getName());
    } catch (CoreException e) {
        logger.put(Messages.EM0100, e);
    } catch (IOException e) {
        logger.put(Messages.EM0006, e, output.getName());
    }
}

From source file:com.iflytek.edu.cloud.frame.spring.Jaxb2RootElementHttpMessageConverterExt.java

@Override
protected void customizeMarshaller(Marshaller marshaller) {
    try {/*from  w  w w .  j av  a 2s.c  o m*/
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    } catch (PropertyException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T//  www .  ja  v a 2 s. c o  m
 * @param object the object to be transformed into XML
 * @param writer the writer object
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, Writer writer) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName());

    Marshaller m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(element, writer);
}

From source file:eu.planets_project.tb.impl.serialization.JaxbUtil.java

/**
* Marshalling via Jaxb: Creates a String Serialization of the requested class for
* the content of Object objectToSerialize. The provided object and the requested class need to be of the same Type
* @param <T>/*from   w w w.jav  a 2  s .  co  m*/
* @param objectClass
* @param objectToSerialize
* @return
* @throws Exception
*/
public static <T> String marshallObjectwithJAXB(Class<T> objectClass, T objectToSerialize) throws Exception {
    JAXBContext context;
    try {
        context = JAXBContext.newInstance(objectClass);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        //now call the actual marshalling job
        m.marshal(objectToSerialize, sw);
        return sw.toString();
    } catch (Exception e) {
        log.error("marshalWorkflowResult failed for objectClass: " + objectClass + " with " + e);
        throw e;
    }
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

public static String marshallToString(Class sourceClass, Object source) {
    try {//from  w w w.  j  a va2s  . c om
        JAXBContext jaxbContext = JAXBContext.newInstance(sourceClass);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:Main.java

/**
 * Creates a {@link Marshaller} based on the given {@link JAXBContext}
 * and configures it to use the given {@link Charset}, and allows to
 * output the XML code to be generated formatted and as an XML fragment.
 * /*from ww  w .j a v  a 2 s  . com*/
 * @param ctx the {@link JAXBContext} to create a {@link Marshaller} for
 * @param charset the {@link Charset} the XML code should be formatted
 * @param formatted {@code true} if the XML code should be formatted,
 *       {@code false} otherwise
 * @param fragment {@code false} if the XML code should start with
 *       {@code <?xml }, or {@code true} if just fragment XML code should
 *       get generated
 * 
 * @return a preconfigured {@link Marshaller}
 * 
 * @throws IOException in case no {@link Marshaller} could get created
 */
public static Marshaller createMarshaller(JAXBContext ctx, Charset charset, boolean formatted, boolean fragment)
        throws IOException {
    if (charset == null) {
        return createMarshaller(ctx, Charset.defaultCharset(), formatted, fragment);
    }
    Objects.requireNonNull(ctx);
    try {
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
        return marshaller;
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}

From source file:ejava.projects.edmv.xml.EDmvBindingTest.java

public void setUp() throws Exception {
    JAXBContext jaxbc = JAXBContext.newInstance(Dmv.class);
    m = jaxbc.createMarshaller();/*from  w w w . java 2  s. c o m*/
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

}

From source file:Main.java

public static <T> String write(T content, Class<T> typeParameterClass) {
    ByteArrayOutputStream baos = null;
    try {//from  ww w  . j  ava2  s  .  c om
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
        jaxbMarshaller.marshal(content, osw);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return baos != null ? baos.toString() : null;
}

From source file:labr_client.xml.ObjToXML.java

public static void jaxbObjectToXML(LabrRequest request, HashMap<String, String> patient) {

    try {//w ww  . j a v a  2 s .  c o  m
        JAXBContext context = JAXBContext.newInstance(LabrRequest.class);
        Marshaller m = context.createMarshaller();
        //for pretty-print XML in JAXB
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        //----------PATIENT----------------------------
        request.patient.name.setValue("De Mey");
        request.patient.name.setX(20);
        request.patient.name.setY(50);
        request.patient.firstName.setValue("Matthias");
        request.patient.firstName.setX(150);
        request.patient.firstName.setY(50);
        request.patient.birthDate.setValue("07/06/1990");
        request.patient.birthDate.setX(20);
        request.patient.birthDate.setY(80);
        request.patient.gender.setValue("M");
        request.patient.gender.setX(150);
        request.patient.gender.setY(80);
        request.patient.straatAndNumber.setValue("Hemelbeekstraat 16");
        request.patient.straatAndNumber.setX(20);
        request.patient.straatAndNumber.setY(110);
        request.patient.zip.setValue("8000");
        request.patient.zip.setX(20);
        request.patient.zip.setY(140);
        request.patient.city.setValue("Brugge");
        request.patient.city.setX(120);
        request.patient.city.setY(140);

        request.patient.country.setValue("Belgie");
        request.patient.country.setX(20);
        request.patient.country.setY(170);
        request.patient.nationalNumber.setValue("001-12345-223");
        request.patient.nationalNumber.setX(120);
        request.patient.nationalNumber.setY(210);
        //----------ATTRIBUTEN-------------------------
        request.attributes.setAge(26);
        //----------LABELS-----------------------------
        LabrXMLLabel l = new LabrXMLLabel();
        l.setColor("#110000");
        l.setSize(12);
        l.setId("1");
        l.setValue("Hematologie");
        request.labels.getLabel().add(l);
        //---------KNOPPEN-----------------------------
        request.buttons.save.setX(300);
        request.buttons.save.setY(100);
        request.buttons.save.setColor(-65536);
        request.buttons.save.setWidth(100);
        request.buttons.save.setValue("Save");
        //----------AANVRAGEN--------------------------
        LabrXMLRequest r = new LabrXMLRequest();
        r.setComment("Dit is commentaar");
        r.setLoinc("Kalium");
        request.requests.getRequest().add(r);
        r.setLoinc("Natrium");
        request.requests.getRequest().add(r);
        //----------AANVRAGEN--------------------------
        // Write to System.out for debugging
        // m.marshal(emp, System.out);            
        // Write to File
        m.marshal(request, new File("C:\\Users\\labbl\\Documents\\test.xml"));
    } catch (JAXBException e) {
        e.printStackTrace();
    }

}