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:de.avanux.smartapplianceenabler.semp.webservice.SempController.java

private String marshall(Device2EM device2EM) {
    StringWriter writer = new StringWriter();
    try {//from  w  w  w .  jav a 2 s .  c om
        JAXBContext context = JAXBContext.newInstance(Device2EM.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(device2EM, writer);
        return writer.toString();
    } catch (JAXBException e) {
        logger.error("Error marshalling", e);
    }
    return null;
}

From source file:edu.harvard.lib.lcloud.ItemDAO.java

/**
 * /*from w  ww.jav a2s  .c  o m*/
 * Marshals a SearchResult or ModsType object to an XML string. This string
 * is converted to json using the org.json package. Order of the json array is
 * not guaranteed (as per spec); TO DO (201405007) - investigate means for
 * preserving XML order (json schema?)
 * 
 * @param obj a a SearchResult or ModsType object for conversion to json string
 * @return      the json String
 */
protected String writeJson(Object obj) throws JAXBException {
    StringWriter sw = new StringWriter();
    String jsonString = null;
    Marshaller jaxbMarshaller = JAXBHelper.context.createMarshaller();
    jaxbMarshaller.marshal(obj, sw);

    try {
        String xml = sw.toString();
        JSONObject xmlJSONObj = XML.toJSONObject(xml);
        jsonString = xmlJSONObj.toString(5);
        ;
        System.out.println(jsonString);
    } catch (JSONException je) {
        log.error(je.getMessage());
        je.printStackTrace();
    }
    return jsonString;

}

From source file:com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlistParser.java

private void marshallPlist(Plist plist, Writer projectFile) throws JAXBException {
    JAXBContext ctx = JAXBContext.newInstance(com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlist.class);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    try {/*  w  ww  . j ava  2s.  c  o  m*/
        marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", xmlHeaders);
    } catch (PropertyException ex) {
        marshaller.setProperty("com.sun.xml.bind.xmlHeaders", xmlHeaders);
    }
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.marshal(plist, projectFile);
}

From source file:it.cnr.icar.eric.client.xml.registry.infomodel.IdentifiableImpl.java

public String toXML() throws JAXRException {
    try {//  ww  w.j a  va2 s.c om
        StringWriter sw = new StringWriter();
        Marshaller marshaller = BindingUtility.getInstance().getJAXBContext().createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        JAXBElement<IdentifiableType> ebIdentifiable = bu.rimFac
                .createIdentifiable((IdentifiableType) toBindingObject());
        marshaller.marshal(ebIdentifiable, sw);

        return sw.toString();
    } catch (javax.xml.bind.JAXBException e) {
        throw new JAXRException(e);
    }
}

From source file:com.castlemock.web.basis.model.RepositoryImpl.java

/**
 * The method provides the functionality to export an entity and convert it to a String
 * @param id The id of the entityy that will be converted and exported
 * @return The entity with the provided id as a String
 *//*from   ww w .j av  a 2 s.  c  o m*/
@Override
public String exportOne(final I id) {
    try {
        final T type = collection.get(id);
        final JAXBContext context = JAXBContext.newInstance(entityClass);
        final Marshaller marshaller = context.createMarshaller();
        final StringWriter writer = new StringWriter();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(type, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IllegalStateException("Unable to export type", e);
    }
}

From source file:de.fischer.thotti.core.runner.NDRunner.java

protected void saveTestResult(TestSuiteResult result) {
    Package pkg = TestSuiteResult.class.getPackage();

    File resultFile = generateResultFileName();

    JAXBContext jaxbContext = null;

    try {//w ww  . jav  a 2s  . c om
        jaxbContext = JAXBContext.newInstance(pkg.getName());

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        marshaller.marshal(result, resultFile);
    } catch (JAXBException e) {
        // @todo?
    }
}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterOutbound.java

@Override
public void receive(GeoEvent geoEvent) {

    stringBuffer.setLength(0);/*from ww w.  j  av a  2 s . co  m*/
    GeoEventDefinition definition = geoEvent.getGeoEventDefinition();
    System.out.println("Creating Event to marshal...");
    Event event = new Event();
    for (FieldDefinition fieldDefinition : definition.getFieldDefinitions()) {
        try {
            String attributeName = fieldDefinition.getName();
            Object value;
            if ((value = geoEvent.getField(attributeName)) != null) {

                if (attributeName.equalsIgnoreCase("version")) {
                    event.setVersion((Double) value);
                } else if (attributeName.equalsIgnoreCase("uid")) {
                    event.setUid(value.toString());
                } else if (attributeName.equalsIgnoreCase("type")) {
                    event.setType(value.toString());
                } else if (attributeName.equalsIgnoreCase("how")) {
                    event.setHow(value.toString());
                } else if (attributeName.equalsIgnoreCase("time")) {
                    event.setTime((Date) value);
                } else if (attributeName.equalsIgnoreCase("start")) {
                    event.setStart((Date) value);
                } else if (attributeName.equalsIgnoreCase("stale")) {
                    event.setStale((Date) value);
                } else if (attributeName.equalsIgnoreCase("access")) {
                    event.setAccess(value.toString());
                } else if (attributeName.equalsIgnoreCase("opex")) {
                    event.setOpex(value.toString());
                } else if (attributeName.equalsIgnoreCase("qos")) {
                    event.setQos(value.toString());
                } else if (attributeName.equalsIgnoreCase("detail")) {
                    event.setDetail(this.unpackDetial(fieldDefinition, geoEvent.getFieldGroup("detail")));

                    // GETALLFIELDS
                    // CHECK ITS TYPE IF GROUP THEN INSPECT THEM
                } else if (attributeName.equalsIgnoreCase("point")) {
                    Point p = pointFromJson(value);
                    event.setPoint(pointFromJson(p));
                }
            }

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

    }

    /////////////////////////
    String xmlResult = null;
    StringBuilder myResult = new StringBuilder();
    int content;

    System.out.println("Event created.");

    System.out.println("Marshalling Event into XML.");

    try {

        JAXBContext contextObj = JAXBContext.newInstance(Event.class);
        Marshaller marshallerObj = contextObj.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        marshallerObj.marshal(event, os);
        ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());

        while ((content = bais.read()) != -1) {
            myResult.append((char) content);
        }

        xmlResult = fixEscapeCharacters(myResult.toString());
        System.out.println("**** XML RESULTS ***");
        System.out.println(xmlResult);
        //this.byteListener.receive(ByteBuffer.wrap(xmlResult.getBytes()), "");
        super.receive(ByteBuffer.wrap(xmlResult.getBytes()), "", geoEvent);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Done");
}

From source file:com.moss.veracity.core.data.PersistenceManager.java

private void write(Object o, DatabaseEntry entry) throws JAXBException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(o, out);

    entry.setData(out.toByteArray());/*w  w w .ja v a 2  s  .c om*/
}

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.smartdeployment.QUELLE.QuelleService.java

@POST
@Path("/submitCloudDescription")
@Consumes(MediaType.APPLICATION_XML)/*from  ww w  .j a v a2s .co m*/
public boolean submitCloudProviderDescription(CloudProvider provider,
        @DefaultValue("true") @QueryParam("overwrite") boolean overwrite) {
    File saveAs = new File(SalsaConfiguration.getCloudProviderDescriptionDir() + File.separator
            + provider.getName() + cloudDescriptionFileExtension);
    if (saveAs.exists() && overwrite == false) {
        EngineLogger.logger.debug("Do not overwrite file : " + saveAs);
        return false;
    }
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(CloudProvider.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(provider, saveAs);
    } catch (JAXBException e) {
        EngineLogger.logger.debug("Fail to pass the cloud description !");
        e.printStackTrace();
    }
    EngineLogger.logger.debug("Saved/Updated cloud description in : " + saveAs);
    return true;
}