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:Main.java

public static String convertBean2Xml(Object obj) throws IOException {
    String result = null;/* w  ww  .j  a va  2s .  c om*/
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
                (String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
        xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
        marshaller.marshal(obj, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
        result = baos.toString("UTF-8");

    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    } catch (XMLStreamException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:Main.java

public static <T> Document marshal(Object jaxbElement, Class<T> jaxbFactory) throws JAXBException {
    if (!(jaxbElement instanceof JAXBElement<?>)) {
        throw new JAXBException("Must be a instance of JAXBElement<?>");
    }/*from   w ww.  j  av a 2s. com*/
    Document doc = null;
    try {
        JAXBContext jc = JAXBContext.newInstance(jaxbFactory);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.newDocument();
        marshaller.marshal(jaxbElement, doc);

    } catch (PropertyException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return doc;
}

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.
 * /*  w ww  .  j  a va 2s.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:net.firejack.aws.license.LicenseHelper.java

public static File create(License license) throws IOException, NoSuchAlgorithmException, JAXBException {
    signature(license);//w w w  .ja v  a  2  s. c o  m

    File tmp = new File("/tmp/", license.getName() + ".xml");

    tmp.getParentFile().mkdirs();
    tmp.createNewFile();
    FileOutputStream stream = new FileOutputStream(tmp);

    JAXBContext jaxbContext = JAXBContext.newInstance(License.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(license, stream);
    stream.close();

    return tmp;
}

From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java

public static void setHeader(final Marshaller m) throws PropertyException {
    m.setProperty("com.sun.xml.internal.bind.xmlHeaders", XML_WITHOUT_STANDALONE + DOCTYPE);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> String marshal(T object) throws IOException, JAXBException {
    Class<T> clzz = (Class<T>) object.getClass();
    JAXBContext context;/*from  ww  w.ja v  a2 s. c  o  m*/
    context = JAXBContext.newInstance(clzz);
    Marshaller m = context.createMarshaller();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, os);
    return os.toString();
}

From source file:Main.java

/**
 * Method to write an XML object in the given filePath.
 * /* w  ww  . j av  a  2  s  .c  o  m*/
 * @param <S>
 *            The type of object that needs to written in XML file.
 * @param filePath
 *            Path of file where xml object gets written
 * @param object
 *            The Object that needs to be written in XML file.
 * @throws Exception
 */
public static <S> void writeXMLObject(String filePath, S object) throws Exception {
    // java XML context object.
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    // file.
    File file = new File(filePath);
    // Creating marshaller
    Marshaller marshaller = jc.createMarshaller();
    // Setting output format
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // Marshalling object.
    marshaller.marshal(object, file);
}

From source file:de.hybris.platform.b2b.punchout.PunchOutUtils.java

public static void removeStandalone(final Marshaller marshaller) throws PropertyException {
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
}

From source file:Main.java

public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException {
    Marshaller m = CTX.createMarshaller();
    m.setEventHandler(new DefaultValidationEventHandler());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
    m.marshal(o, os);//  w  w w .  j a  v a2 s.  c o  m
}

From source file:Main.java

/**
 * Object to XML/*  www  .  j  a v a 2 s.c o  m*/
 * 
 * @param object
 * @return
 */
public static String convertToXML(Object object) {
    try {
        System.out.println(mMap.containsKey(object.getClass()) + "---mmap-----");
        if (!mMap.containsKey(object.getClass())) {
            JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            // marshaller.setProperty(CharacterEscapeHandler.class.getName(),
            // new CharacterEscapeHandler() {
            // public void escape(char[] ac, int i, int j, boolean
            // flag,Writer writer) throws IOException {
            // writer.write( ac, i, j ); }
            // });
            mMap.put(object.getClass(), marshaller);
        }
        System.out.println("----mmap--" + mMap.toString());
        StringWriter stringWriter = new StringWriter();
        mMap.get(object.getClass()).marshal(object, stringWriter);
        return stringWriter.getBuffer().toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}