Example usage for javax.xml.bind JAXBContext createMarshaller

List of usage examples for javax.xml.bind JAXBContext createMarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createMarshaller.

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:esg.node.components.registry.ShardsListGleaner.java

public synchronized boolean saveShardsList(Shards shardlist) {
    boolean success = false;
    if (shardlist == null) {
        log.error("shardlist is null ? [" + shardlist + "]");
        return success;
    }/*w w  w  . jav a2s  .  co m*/
    log.trace("Saving SHARDS list information to " + shardsListPath + shardsListFile);
    try {
        JAXBContext jc = JAXBContext.newInstance(Shards.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(shardlist, new FileOutputStream(shardsListPath + shardsListFile));
        success = true;
    } catch (Exception e) {
        log.error(e);
    }

    return success;
}

From source file:org.trpr.platform.integration.impl.xml.XMLTranscoderImpl.java

/**
 * Replacement method that uses JAXB directly instead of via Spring OXM. Provided just as an option
 *///www  . j av  a 2s  . c o  m
private String marshalUsingJAXB(Object object) throws XMLDataException {
    try {
        StringWriter stringWriter = new StringWriter();
        javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext
                .newInstance(object.getClass().getPackage().getName());
        javax.xml.bind.Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, stringWriter);
        return stringWriter.toString();
    } catch (javax.xml.bind.JAXBException e) {
        throw new XMLDataException("Error marshalling Object of type : " + object.getClass().getName(), e);
    }
}

From source file:edu.htwm.vsp.phoebook.rest.client.RestClient.java

/**
 * Example code for serializing a PhoneUser via JAXB.
 *///w  w w.  j  a  va  2s  .  co m
public void marshalPhoneUserToXML(PhoneUser user, Writer out) throws JAXBException {

    JAXBContext jc = JAXBContext.newInstance(PhoneUser.class);
    assertNotNull(jc);
    Marshaller marshaller = jc.createMarshaller();

    assertNotNull(marshaller);

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
    marshaller.marshal(user, out);
}

From source file:ejava.projects.edmv.bl.DataGen.java

public void generate(Writer writer) throws JAXBException, DatatypeConfigurationException {
    dtf = DatatypeFactory.newInstance();
    JAXBContext jaxbc = JAXBContext.newInstance(Dmv.class);
    Marshaller m = jaxbc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Dmv dmvDoc = new Dmv();
    dmvDoc.setPeople(new PersonsType());
    dmvDoc.setVehicleRegistrations(new VehicleRegistrationsType());

    //get all people
    int index = 0;
    List<DMVPerson> people;
    do {//from   www . j  a v a 2 s.c  o  m
        people = personDAO.getPeople(index, 10);
        for (DMVPerson p : people) {
            dmvDoc.getPeople().getPerson().add(createXMLPerson(p));
        }
        index += people.size();
    } while (people.size() > 0);
    log.debug("found " + index + " total people");

    //get all vehicle registrations
    index = 0;
    List<DMVVehicleRegistration> vRegs;
    do {
        vRegs = vehicleDAO.getRegistrations(index, 10);
        for (DMVVehicleRegistration vReg : vRegs) {
            dmvDoc.getVehicleRegistrations().getVehicleRegistration()
                    .add(createXMLRegistration(vReg, dmvDoc.getPeople()));
        }
        index += vRegs.size();
    } while (vRegs.size() > 0);
    log.debug("found " + index + " total registrations");

    m.marshal(dmvDoc, writer);
}

From source file:com.u2apple.tool.dao.DeviceXmlDaoJaxbImpl.java

private void flushDevices() throws JAXBException, PropertyException {
    if (isDeviceChanged) {
        File file = new File(Configuration.getProperty(Constants.DEVICES_XML));
        JAXBContext jaxbContext = JAXBContext.newInstance(StaticMapFile.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(getStaticMapFile(), file);
        isDeviceChanged = false;/*from  w  ww.  j a v  a 2s . c o  m*/
    }
}

From source file:com.t2tierp.controller.nfe.CancelaNfe.java

private String xmlCancelamento(br.inf.portalfiscal.nfe.retevento.TRetEnvEvento retorno, String versaoDados,
        String codigoUf, String ambiente, String chaveAcesso, String numeroProtocolo, String justificativa,
        String cnpj, String dataHoraEvento) throws Exception {
    TProcEvento xml = new TProcEvento();
    xml.setVersao(versaoDados);/*from w ww .ja v a  2  s  .c  om*/

    TEvento evento = new TEvento();
    xml.setEvento(evento);
    TEvento.InfEvento infEvento = new TEvento.InfEvento();
    evento.setInfEvento(infEvento);
    evento.setVersao(versaoDados);
    TEvento.InfEvento.DetEvento detEvento = new TEvento.InfEvento.DetEvento();
    infEvento.setDetEvento(detEvento);

    infEvento.setCNPJ(cnpj);
    infEvento.setCOrgao(codigoUf);
    infEvento.setChNFe(chaveAcesso);
    infEvento.setDhEvento(dataHoraEvento);
    infEvento.setId("ID" + "110111" + chaveAcesso + "01");
    infEvento.setNSeqEvento("1");
    infEvento.setTpAmb(ambiente);
    infEvento.setTpEvento("110111");
    infEvento.setVerEvento(versaoDados);

    detEvento.setDescEvento("Cancelamento");
    detEvento.setNProt(numeroProtocolo);
    detEvento.setVersao(versaoDados);
    detEvento.setXJust(justificativa);

    br.inf.portalfiscal.nfe.procevento.TRetEvento retEvento = new br.inf.portalfiscal.nfe.procevento.TRetEvento();
    xml.setRetEvento(retEvento);
    br.inf.portalfiscal.nfe.procevento.TRetEvento.InfEvento retInfEvento = new br.inf.portalfiscal.nfe.procevento.TRetEvento.InfEvento();
    retEvento.setInfEvento(retInfEvento);

    retEvento.setVersao(retorno.getVersao());
    retInfEvento.setCPFDest(retorno.getRetEvento().get(0).getInfEvento().getCPFDest());
    retInfEvento.setCNPJDest(retorno.getRetEvento().get(0).getInfEvento().getCNPJDest());
    retInfEvento.setCOrgao(retorno.getRetEvento().get(0).getInfEvento().getCOrgao());
    retInfEvento.setCStat(retorno.getRetEvento().get(0).getInfEvento().getCStat());
    retInfEvento.setChNFe(retorno.getRetEvento().get(0).getInfEvento().getChNFe());
    retInfEvento.setDhRegEvento(retorno.getRetEvento().get(0).getInfEvento().getDhRegEvento());
    retInfEvento.setEmailDest(retorno.getRetEvento().get(0).getInfEvento().getEmailDest());
    retInfEvento.setId(retorno.getRetEvento().get(0).getInfEvento().getId());
    retInfEvento.setNProt(retorno.getRetEvento().get(0).getInfEvento().getNProt());
    retInfEvento.setNSeqEvento(retorno.getRetEvento().get(0).getInfEvento().getNSeqEvento());
    retInfEvento.setTpAmb(retorno.getRetEvento().get(0).getInfEvento().getTpAmb());
    retInfEvento.setTpEvento(retorno.getRetEvento().get(0).getInfEvento().getTpEvento());
    retInfEvento.setVerAplic(retorno.getRetEvento().get(0).getInfEvento().getVerAplic());
    retInfEvento.setXEvento(retorno.getRetEvento().get(0).getInfEvento().getXEvento());
    retInfEvento.setXMotivo(retorno.getRetEvento().get(0).getInfEvento().getXMotivo());

    JAXBContext jc = JAXBContext.newInstance("br.inf.portalfiscal.nfe.procevento");
    Marshaller marshaller = jc.createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    JAXBElement<TProcEvento> element = new br.inf.portalfiscal.nfe.procevento.ObjectFactory()
            .createProcEventoNFe(xml);

    StringWriter writer = new StringWriter();
    marshaller.marshal(element, writer);

    return writer.toString();
}

From source file:hydrograph.ui.engine.util.ConverterUtil.java

private void validateJobState(Graph graph, boolean validate, IFileStore externalOutputFile,
        ByteArrayOutputStream out) throws CoreException, JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(graph.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    out = new ByteArrayOutputStream();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(graph, out);/* w ww  .ja v a  2  s .c  o m*/
    out = ComponentXpath.INSTANCE.addParameters(out);
}

From source file:com.azaptree.services.spring.application.config.SpringApplicationServiceConfig.java

private void log(final SpringApplicationService config) {
    final StringWriter sw = new StringWriter();
    final Marshaller m;
    try {//w  w  w .  j a va  2  s  .  com
        final JAXBContext ctx = JAXBContext.newInstance(SpringApplicationService.class.getPackage().getName());
        m = ctx.createMarshaller();
    } catch (final JAXBException e) {
        throw new IllegalStateException("Failed to create marshller ", e);
    }

    try {
        m.marshal(config, sw);
    } catch (final JAXBException e) {
        throw new IllegalStateException("Failed to marshal SpringApplicationService", e);
    }

    LoggerFactory.getLogger(getClass()).info("XML config:\n{}", XmlUtils.prettyFormatXml(sw.toString(), 4));
}

From source file:esg.node.components.registry.AtsWhitelistGleaner.java

public synchronized boolean saveAtsWhitelist(AtsWhitelist atss) {
    boolean success = false;
    if (atss == null) {
        log.error("atss (whitelist) is null ? [" + atss + "]");
        return success;
    }// w ww .  j a v a 2 s  . com
    log.trace("Saving ATS Whitelist information to " + atsWhitelistPath + atsWhitelistFile);
    try {
        JAXBContext jc = JAXBContext.newInstance(AtsWhitelist.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(atss, new FileOutputStream(atsWhitelistPath + atsWhitelistFile));
        success = true;
    } catch (Exception e) {
        log.error(e);
    }

    return success;
}

From source file:hydrograph.ui.engine.util.ConverterUtil.java

/**
 * Store file into local file system./*w  w  w .  ja  v  a 2  s. co m*/
 *
 * @param graph
 * @param externalOutputFile
 * @param out
 * @throws CoreException the core exception
 * @throws JAXBException the JAXB exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void storeFileIntoLocalFileSystem(Graph graph, boolean validate, IFileStore externalOutputFile,
        ByteArrayOutputStream out) throws CoreException, JAXBException, IOException {

    File externalFile = externalOutputFile.toLocalFile(0, null);
    OutputStream outputStream = new FileOutputStream(externalFile.getAbsolutePath().replace(".job", ".xml"));

    JAXBContext jaxbContext = JAXBContext.newInstance(graph.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    out = new ByteArrayOutputStream();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(graph, out);
    out = ComponentXpath.INSTANCE.addParameters(out);
    out.writeTo(outputStream);
    outputStream.close();
}