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 String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

From source file:de.xirp.profile.ProfileGenerator.java

/**
 * Generates a PRO file with the given path from the given 
 * {@link de.xirp.profile.Profile profile} bean.
 * /*from  w ww.jav a  2 s  . co m*/
 * @param profile
 *          The profile to generate the XML for.        
 * @param proFile
 *          The file to write the result to. Must be the full path.
 *            
 * @throws JAXBException if the given profile was null, or something 
 *                    went wrong generating the xml.
 * @throws FileNotFoundException if something went wrong generating the xml.
 * 
 * @see de.xirp.profile.Profile
 */
public static void generatePRO(Profile profile, File proFile) throws JAXBException, FileNotFoundException {

    if (profile == null) {
        throw new JAXBException(I18n.getString("ProfileGenerator.exception.profileNull")); //$NON-NLS-1$
    }

    String fileName = FilenameUtils.getBaseName(proFile.getName());

    JAXBContext jc = JAXBContext.newInstance(Profile.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(profile, new FileOutputStream(
            Constants.CONF_PROFILES_DIR + File.separator + fileName + Constants.PROFILE_POSTFIX));
}

From source file:de.xirp.profile.ProfileGenerator.java

/**
 * Generates a BOT file with the given path from the given 
 * {@link de.xirp.profile.Robot robot} bean.
 * /*from  ww w.java2s  .co m*/
 * @param robot
 *          The robot to generate the XML for. 
 * @param botFile
 *          The file to write the result to. Must be the full path.
 * 
 * @throws JAXBException if the given robot was null, or something 
 *                    went wrong generating the xml.
 * @throws FileNotFoundException if something went wrong generating the xml.
 */
public static void generateBOT(Robot robot, File botFile) throws FileNotFoundException, JAXBException {

    if (robot == null) {
        throw new JAXBException(I18n.getString("ProfileGenerator.exception.robotNull")); //$NON-NLS-1$
    }

    String fileName = FilenameUtils.getBaseName(botFile.getName());

    JAXBContext jc = JAXBContext.newInstance(Robot.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(robot, new FileOutputStream(
            Constants.CONF_ROBOTS_DIR + File.separator + fileName + Constants.ROBOT_POSTFIX));
}

From source file:de.xirp.profile.ProfileGenerator.java

/**
 * Generates a BOT file with the given path from the given 
 * {@link de.xirp.profile.CommunicationSpecification comm-spec} 
 * bean./*  w  w  w.  j a  v  a2 s .  co m*/
 * 
 * @param commSpec
 *          The comm-spec to generate the XML for. 
 * @param cmsFile
 *          The file to write the result to. Must be the full path.
 * 
 * @throws JAXBException if the given comm-spec was null, or something 
 *                    went wrong generating the xml.
 * @throws FileNotFoundException if something went wrong generating the xml.
 */
public static void generateCMS(CommunicationSpecification commSpec, File cmsFile)
        throws JAXBException, FileNotFoundException {

    if (commSpec == null) {
        throw new JAXBException(I18n.getString("ProfileGenerator.exception.comSpecNull")); //$NON-NLS-1$
    }

    String fileName = FilenameUtils.getBaseName(cmsFile.getName());

    JAXBContext jc = JAXBContext.newInstance(CommunicationSpecification.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(commSpec, new FileOutputStream(
            Constants.CONF_COMMSPECS_DIR + File.separator + fileName + Constants.COMM_SPEC_POSTFIX));
}

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

/**
 * @param exp/*w w w . j  a  v  a2  s.  co m*/
 * @param out
 */
private static void writeToOutputStream(ExperimentImpl exp, OutputStream out) {
    try {
        JAXBContext jc = JAXBContext.newInstance(PACKAGE_CONTEXT);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(exp, out);
    } catch (JAXBException e) {
        log.fatal("Writing Experiment to XML failed: " + e);
    }
}

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:at.ac.tuwien.dsg.comot.m.common.Utils.java

public static String asXmlString(Object obj, String contextPath) throws JAXBException {

    StringWriter w = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(contextPath);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(obj, w);

    return w.toString();
}

From source file:de.iteratec.iteraplan.businesslogic.service.SavedQueryXmlHelper.java

/**
 * Serializes a JAXB object to an XML string.
 * //from ww  w . j  a  va2 s.co m
 * @param schema Schema to use.
 * @param xml JAXB object to serialize.
 * @return the XML representation of the passed object
 */
public static String writeQueryToXMLString(String schema, SerializedQuery<?> xml) {
    try {
        Marshaller marshaller = getMarshaller(xml.getClass(), schema);
        StringWriter sw = new StringWriter();
        marshaller.marshal(xml, sw);

        return sw.toString();
    } catch (JAXBException e) {
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR, e);
    } catch (SAXException e) {
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR, e);
    }
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

public static String asXmlString(Object obj, Class<?>... clazz) throws JAXBException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(clazz));
    list.add(obj.getClass());//from   ww  w . j ava 2  s . c  o  m

    StringWriter w = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(list.toArray(new Class[list.size()]));

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(obj, w);

    return w.toString();
}

From source file:Main.java

public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException {
    String pkg = o.getClass().getPackage().getName();
    JAXBContext jc = getCachedContext(pkg);
    Marshaller m = jc.createMarshaller();
    m.setEventHandler(new DefaultValidationEventHandler());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.marshal(o, os);
}