Example usage for javax.xml.bind JAXB unmarshal

List of usage examples for javax.xml.bind JAXB unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind JAXB unmarshal.

Prototype

public static <T> T unmarshal(Source xml, Class<T> type) 

Source Link

Document

Reads in a Java object tree from the given XML input.

Usage

From source file:loanbroker.normalizer.NormalizerOurSoapXmlBank.java

public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException {
    //Connection/*from w  ww.  jav a2s.c o  m*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    //Consumer
    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    //s channel.queueBind(queueName, EXCHANGE_NAME, "OurSoapXmlBank");
    channel.queueBind(queueName, EXCHANGE_NAME, "");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, true, consumer);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    //Producer
    Channel channelOutput = connection.createChannel();
    channelOutput.exchangeDeclare("TeamFirebug", "direct");

    LoanResponse loanResponse;
    DtoOurSoapXmlBank dtoOurSoapXmlBank;
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();

        dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message), DtoOurSoapXmlBank.class);
        loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(), dtoOurSoapXmlBank.getInterestRate(),
                "Our Soap Xml bank", delivery.getProperties().getCorrelationId());
        // loanResponse.setBank(routingKey);

        System.out.println("renter: " + loanResponse.getInterestRate());
        System.out.println("ssn: " + loanResponse.getSsn());
        System.out.println("bank : " + loanResponse.getBank());
        JSONObject jsonObj = new JSONObject(loanResponse);
        // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes());
        channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null,
                jsonObj.toString().getBytes());
    }
}

From source file:loanbroker.normalizer.NormalizerTeachersXmlBank.java

public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException {
    //Connection//from w w  w.  j a v a2s  .co m
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    //Consumer
    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    //String queueName = channel.queueDeclare().getQueue();
    //s channel.queueBind(queueName, EXCHANGE_NAME, "OurSoapXmlBank");
    channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(RPC_QUEUE_NAME, true, consumer);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    //Producer
    Channel channelOutput = connection.createChannel();
    channelOutput.exchangeDeclare("TeamFirebug", "direct");

    while (true) {
        System.out.println("Reading");
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();

        DtoTeachersXmlBank dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message),
                DtoTeachersXmlBank.class);
        LoanResponse loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(),
                dtoOurSoapXmlBank.getInterestRate(), "Teachers Xml Bank",
                delivery.getProperties().getCorrelationId());
        // loanResponse.setBank(routingKey);
        System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

        System.out.println("renter: " + loanResponse.getInterestRate());
        System.out.println("ssn: " + loanResponse.getSsn());
        System.out.println("bank : " + loanResponse.getBank());
        JSONObject jsonObj = new JSONObject(loanResponse);
        System.out.println("JSON:" + jsonObj);
        // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes());
        channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null,
                jsonObj.toString().getBytes());
        delivery = null;
    }
}

From source file:Main.java

public static <T> T unMarshal(String xml, Class<T> clazz) {
    return JAXB.unmarshal(new StringReader(xml), clazz);
}

From source file:Main.java

public static <T> T unmarshal(String xml, Class<T> beanClass) {
    StringReader reader = new StringReader(xml);
    return JAXB.unmarshal(reader, beanClass);
}

From source file:Main.java

/**
 * Unmarshal an arbitrary object with JAXB.
 *
 * @param stream Input stream to read from
 * @param clazz Class of object//  ww w.  j a  v  a2 s. co  m
 */
public static <O> O unmarshal(InputStream stream, Class<O> clazz) throws Exception {
    return JAXB.unmarshal(stream, clazz);
}

From source file:Main.java

/**
 * Return an object for xml/*from  www.  j a  va  2s .  c  o m*/
 * 
 * @param cls
 * @param xml
 * @return
 */
public static Object objectFromXML(Class<?> cls, String xml) {

    return JAXB.unmarshal(new StringReader(xml), cls);
}

From source file:com.l2jserver.tool.conversor.itemtemplate.ItemTemplateConversor.java

private static void processFile(String file) throws FileNotFoundException {
    ItemListXml xml = JAXB.unmarshal(new FileInputStream(file), ItemListXml.class);
    for (final ItemXml item : xml.getItems()) {
        String template = ItemTemplateConversor.template;
        String values = "";
        for (final ItemValueXml val : item.getValues()) {
            values += generateSet(val.getName(), val.getValue());
        }//  ww  w.j  a va2 s.c  om
        template = replace(template, "values", values);
        template = replace(template, "package", "");
        template = replace(template, "import", "");
        template = replace(template, "className", camelCase(item.getName()));
        template = replace(template, "extends", " extends " + camelCase(item.getType()) + "Template");
        template = replace(template, "implements", "");
        template = replace(template, "id", item.getId() + "");
    }
}

From source file:com.meschbach.psi.util.rest.JAXBResponseHandler.java

public void handleResponse(HttpResponse response, HttpStatusCode stautsCode) throws PSIException {
    try {/* w w w .ja va  2  s.  com*/
        HttpEntity entity = response.getEntity();
        T result = JAXB.unmarshal(entity.getContent(), type);
        response(result);
    } catch (IOException ioe) {
        throw new PSIException("Failed to handle unmarshalled response of type " + type.getName(), ioe);
    }
}

From source file:Main.java

private static <T> T deSerialize(Class<T> docClass, StreamSource source) throws JAXBException {
    return JAXB.unmarshal(source, docClass);
}

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);/*from   ww w.  jav  a2s. co  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();
}