Example usage for javax.xml.bind Marshaller setProperty

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

Introduction

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

Prototype

public void setProperty(String name, Object value) throws PropertyException;

Source Link

Document

Set the particular property in the underlying implementation of Marshaller .

Usage

From source file:com.redhat.akashche.wixgen.cli.Launcher.java

private static Marshaller createMarshaller() throws Exception {
    JAXBContext jaxb = JAXBContext.newInstance(Wix.class.getPackage().getName());
    Marshaller marshaller = jaxb.createMarshaller();
    marshaller.setProperty(JAXB_FORMATTED_OUTPUT, true);
    return marshaller;
}

From source file:Main.java

/**
 *
 * @param obj/*  w  w  w.j  a v  a 2s . c o  m*/
 * @return
 * @throws JAXBException
 */
public static String serializeToString(Object obj) throws JAXBException {
    java.io.StringWriter sw = new StringWriter();
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, sw);
    return sw.toString();
}

From source file:org.vertx.java.http.eventbusbridge.util.SerializationHelper.java

private static String toXml(final Object xmlObject) throws JAXBException {
    StringWriter writer = new StringWriter();
    Marshaller jaxbMarshaller = JAXB_CONTEXT.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(xmlObject, writer);
    return writer.toString();
}

From source file:Main.java

/**
 * Create a marshaller for XML generation.
 *
 * @param encoding/* w  ww  .j a  va 2s  .  co m*/
 * encoding of generated XML output
 *
 * @param prettyPrint
 * enable pretty printing for generated XML output
 *
 * @return
 * marshaller
 *
 * @throws JAXBException
 * if the marshaller is not creatable
 */
public static Marshaller createMarshaller(String encoding, boolean prettyPrint) throws JAXBException {
    Marshaller m = getContext().createMarshaller();
    m.setProperty(Marshaller.JAXB_ENCODING, encoding);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, prettyPrint);
    return m;
}

From source file:Main.java

/**
 * Return the xml translation of an object
 * //from  ww w  . ja  v a  2 s  .c om
 * @param cls
 * @param entity
 * @return
 */
public static String objectToXML(Class<?> cls, Object entity) {

    try {
        Marshaller m = JAXBContext.newInstance(cls).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        m.marshal(entity, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.vincibean.salestaxes.jaxb.JaxbFactory.java

/**
 * Factory method, creates a {@link Marshaller} from the context given in the constructor; moreover, ensure that
 * the marshalled XML data is formatted with linefeeds and indentation.  
 * @return an {@link Optional} object which may or may not contain a {@link Marshaller}
 *///from  w  w  w .  ja v  a 2  s  .  com
public static Optional<Marshaller> createMarshaller(final Class<?> context) {
    try {
        Marshaller marshaller = JAXBContext.newInstance(context).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new ClassPathResource("receipt/Poiuyt.xsd").getFile()));
        marshaller.setEventHandler(new FoobarValidationEventHandler());
        return Optional.fromNullable(marshaller);
    } catch (JAXBException | SAXException | IOException e) {
        logger.warn("Exception on jaxb factory creation: ", e);
        return Optional.absent();
    }
}

From source file:Main.java

private static <T> Marshaller createMarshaller(Class<T> clazz, Marshaller.Listener listener)
        throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Marshaller marshaller = jc.createMarshaller();
    if (listener != null) {
        marshaller.setListener(listener);
    }/* www  .j  av  a 2s .c om*/
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    return marshaller;
}

From source file:Main.java

public static <T> void saveObject(T object, Class<T> typeClass, String path) {
    try {//from   w w  w .ja  v a2  s. co m
        File file = new File(path);
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(object, file);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static <T> void saveObject(T object, Class<T> typeClass, URL path) {
    try {/*from  w  w  w.  j ava2 s . c om*/
        File file = new File(path.toURI());
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(object, file);

    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

From source file:io.konik.utils.InvoiceLoaderUtils.java

public static Marshaller createZfMarshaller() throws JAXBException {
    Marshaller marshaller = newInstance("io.konik.zugferd").createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, TRUE);
    return marshaller;
}