Example usage for javax.xml.bind Marshaller marshal

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

Introduction

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

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:Main.java

public static void saveInstance(OutputStream outputStream, Object instance)

        throws JAXBException, IOException {

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING);

    marshaller.marshal(instance, outputStream);

    outputStream.flush();/*from w  w  w  . jav  a2s .c om*/

}

From source file:Main.java

public static void saveInstance(Writer output, Object instance)

        throws JAXBException, IOException {

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING);

    marshaller.marshal(instance, output);

    output.flush();//from  ww  w. j a  va 2 s.co  m

}

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

/**
 * JsCheckOptionsxml????./*from w w w  . ja v  a2  s .c om*/
 * 
 * 
 * @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:Main.java

public static String getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema)
        throws JAXBException {
    String packageName = versioningInfo.getValue().getClass().getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(packageName);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatXml);

    if (schema != null) {
        marshaller.setSchema(schema);// ww w .  ja  va 2  s.  c  o  m
    }

    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    marshaller.marshal(versioningInfo, oStream);

    return oStream.toString();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static void marshal(Object object, Writer w) throws JAXBException {
    Class clazz = object.getClass();
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);/*  www  .j a  v a  2 s.co  m*/
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setSchema(null);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setEventHandler(valEventHndlr);

    try {
        marshaller.marshal(object, w);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new MarshalException(e.getMessage(), e);
        }
    }
    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Marshall Exception if there is a parsing error
                throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException());
            }
        }
    }
}

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  2s  .  com
* @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:Main.java

/**
 * Marshal a JAXB element to a XML file/*from   w w  w . j a v a 2  s.  c  om*/
 *
 * @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 void saveInstance(OutputStream outputStream, URL schemaURL, String schemaName, Object instance)
        throws JAXBException,

        IOException {/*ww  w  .  j  a va 2 s  .  c  om*/

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaURL + " " + schemaName);

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.marshal(instance, outputStream);

    outputStream.flush();

}

From source file:Main.java

/**
 * Return the xml translation of an object
 * //from w  w w.java 2  s  . com
 * @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:com.netflix.imfutility.dpp.audio.AudioMapHelper.java

public static void writeAudioMapToFile(File outputFile, AudioMapType audioMap) {
    JAXBContext jaxbContext;/* www .  ja  v a  2 s  .  com*/
    try {
        jaxbContext = JAXBContext.newInstance(AudioMapType.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<AudioMapType> audioMapJaxb = new ObjectFactory().createAudioMap(audioMap);
        jaxbMarshaller.marshal(audioMapJaxb, outputFile);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}