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

/**
 * Writes the content of the specified object into the specified XML file.
 * @param filename            Path to the XML file.
 * @param content            Content as an object of the specified class.
 * @param typeParameterClass   Class of the object with the content.
 * @return                  File./* w w  w .  j a  va2s. co m*/
 */
public static <T> File write(File file, T content, Class<T> typeParameterClass) {
    try {
        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);

        jaxbMarshaller.marshal(content, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:Main.java

public static String toXML(Object obj) {
    try {/*from   ww w .  ja va 2  s .  com*/
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static void Object2XmlFile(Object ob, String path) throws JAXBException, FileNotFoundException {
    Class local = ob.getClass();/*from  w  w w.ja v  a2 s  .  c om*/
    JAXBContext context = JAXBContext.newInstance(new Class[] { local });
    Marshaller marshaller = context.createMarshaller();
    // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
    // paramNamespacePrefixMapper);
    // marshaller.setProperty("jaxb.formatted.output",
    // Boolean.valueOf(true));
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(ob, new FileOutputStream(new File(path)));
}

From source file:cz.lbenda.dataman.rc.DbConfigFactory.java

public static String storeToString(Callback<DbConfig, String> cacheDbStructureWriteFactory) {
    cz.lbenda.dataman.schema.dataman.ObjectFactory of = new cz.lbenda.dataman.schema.dataman.ObjectFactory();
    DatamanType config = of.createDatamanType();
    config.setSessions(of.createSessionsType());

    for (DbConfig sc : getConfigurations()) {
        config.getSessions().getSession()
                .add(sc.storeToSessionType(null, cacheDbStructureWriteFactory.call(sc)));
    }/*from  w  w  w .ja v  a2 s.c o  m*/

    try {
        JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class);
        Marshaller m = jc.createMarshaller();
        StringWriter sw = new StringWriter();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(of.createDataman(config), sw);
        return sw.toString();
    } catch (JAXBException e) {
        LOG.error("Problem with write configuration: " + e.toString(), e);
        throw new RuntimeException("Problem with write configuration: " + e.toString(), e);
    }
}

From source file:com.sarm.lonelyplanet.process.DestinationProcessorTest.java

/**
 * This method is not a JUnit test method. This is used to create an XML
 * file of 30000 destinations and to regressively test the UnMarshalling
 * technique used in the LPUnMarshaller. This is called by the test method :
 * testProcessDestinationByStAXfor30000 to test the 30000 destinations.
 *
 * @param destinationFileName/*from   ww  w .  j a v  a  2s .c o m*/
 * @throws JAXBException
 * @throws FileNotFoundException
 * @throws XMLStreamException
 * @throws UnsupportedEncodingException
 */
public static void testMarshall(String destinationFileName)
        throws JAXBException, FileNotFoundException, XMLStreamException, UnsupportedEncodingException {
    DestinationProcessor destinationProcessor = new DestinationProcessor();
    List<Destination> destinations = destinationProcessor.processDestinationByStAX(destinationFileName);
    List<Destination> tempDestinations = new ArrayList<>();
    for (int i = 0; i < 1500; i++) {
        for (Destination destination : destinations) {
            Destination temp = new Destination();
            temp = destination.clone();
            temp.setTitle(temp.getTitle() + i);
            tempDestinations.add(temp);
        }
    }
    Destinations destinationList = new Destinations();
    destinationList.setDestinations(tempDestinations);
    logger.info(" destinationList " + destinationList.getDestinations().size());

    JAXBContext jc = JAXBContext.newInstance(Destinations.class);
    logger.debug(jc.getClass());
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(destinationList, new File(regressDestinationfile));
    logger.info("Successfully created regressDestinationfile " + regressDestinationfile);

}

From source file:Main.java

public static String convertObjectToXML(Object object) {
    JAXBContext jaxbContext = null;
    Marshaller jaxbMarshaller = null;
    StringWriter stringWriter = new StringWriter();
    try {/* ww  w  .  jav a  2 s .  c  o m*/
        jaxbContext = JAXBContext.newInstance(object.getClass());
        jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        jaxbMarshaller.marshal(object, stringWriter);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    String xmlString = stringWriter.toString();

    return xmlString;

}

From source file:cool.pandora.modeller.DocManifestBuilder.java

/**
 * marshal./*from w w  w.j a v a 2 s.  c  om*/
 *
 * @param hocr File
 * @return hocr
 * @throws JAXBException Exception
 */
private static ByteArrayOutputStream marshal(final File hocr) throws JAXBException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final JAXBContext jaxbContext = JAXBContext.newInstance(File.class);
    final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(hocr, out);
    return out;
}

From source file:net.firejack.platform.core.utils.FileUtils.java

public static <T> void writeJAXB(T obj, OutputStream stream, Class... classes)
        throws IOException, JAXBException {

    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setCDataElements(new String[] { CDATA_DESCRIPTION });
    outputFormat.setIndenting(true);//  w w w. j  a  v  a  2s . co m

    XMLSerializer serializer = new XMLSerializer(outputFormat);
    serializer.setOutputByteStream(stream);

    if (classes.length == 0) {
        classes = (Class[]) ArrayUtils.add(classes, obj.getClass());
    }

    JAXBContext jaxbContext = JAXBContext.newInstance(classes);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(obj, serializer.asContentHandler());
}

From source file:com.wavemaker.runtime.ws.HTTPBindingSupport.java

public static String convertToXMLString(Object o) {
    try {// www  . j av a2 s .c o m
        JAXBContext context = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = context.createMarshaller();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        marshaller.marshal(o, os);
        return new String(os.toByteArray());
    } catch (JAXBException e) {
        throw new WebServiceInvocationException(e);
    }
}

From source file:com.biomeris.i2b2.export.ws.messages.MessageBuilder.java

public static String buildPMGetServiceRequest(Network network) throws JAXBException {
    com.biomeris.i2b2.export.datavo.pm.ObjectFactory pmObjectFactory = new com.biomeris.i2b2.export.datavo.pm.ObjectFactory();
    com.biomeris.i2b2.export.ws.messages.extensions.ObjectFactory extObjectFactory = new com.biomeris.i2b2.export.ws.messages.extensions.ObjectFactory();

    ProxyedRequestMessageType proxyedRequestMessageType = new ProxyedRequestMessageType();

    Proxy proxy = new Proxy();
    proxy.setRedirectUrl(network.getPmServiceAddress() + PMSERVICE_NAME);
    proxyedRequestMessageType.setProxy(proxy);

    MessageHeaderType messageHeaderType = new MessageHeaderType();
    SecurityType securityType = new SecurityType();
    securityType.setDomain(network.getDomain());
    securityType.setUsername(network.getUsername());
    PasswordType passwordType = JAXB.unmarshal(new StringReader(network.getPassword()), PasswordType.class);
    securityType.setPassword(passwordType);
    messageHeaderType.setSecurity(securityType);
    messageHeaderType.setProjectId(network.getProject());
    ApplicationType applicationType = new ApplicationType();
    applicationType.setApplicationName("Export Cell");
    applicationType.setApplicationVersion("1.0");
    messageHeaderType.setSendingApplication(applicationType);
    proxyedRequestMessageType.setMessageHeader(messageHeaderType);

    RequestHeaderType requestHeaderType = new RequestHeaderType();
    requestHeaderType.setResultWaittimeMs(1800000);
    proxyedRequestMessageType.setRequestHeader(requestHeaderType);

    BodyType bodyType = new BodyType();

    GetUserConfigurationType g = pmObjectFactory.createGetUserConfigurationType();
    g.getProject().add("undefined");
    JAXBElement<GetUserConfigurationType> any1 = pmObjectFactory.createGetUserConfiguration(g);

    bodyType.getAny().add(any1);//w  w  w . ja v a 2 s  . c  o m
    proxyedRequestMessageType.setMessageBody(bodyType);

    JAXBContext jc = JAXBContext.newInstance(ProxyedRequestMessageType.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter sw = new StringWriter();
    JAXBElement<ProxyedRequestMessageType> xxx = extObjectFactory.createRequestPM(proxyedRequestMessageType);
    m.marshal(xxx, sw);

    return sw.toString();
}