Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

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

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    MyMessage sarm = new MyMessage();
    MyGroupResponse[] sgr = new MyGroupResponse[2];
    sgr[0] = new MyGroupResponse();
    sgr[1] = new MyGroupResponse();
    sarm.setSailingGroup(sgr);/* ww  w.j a  v a2s .  co m*/

    JAXBElement<MyMessage> rootElement = new JAXBElement<MyMessage>(new QName("root"), MyMessage.class, sarm);

    JAXBContext jc = JAXBContext.newInstance(MyMessage.class, MyGroup.class, MyGroupResponse.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(rootElement, System.out);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object value = "Hello World";

    JAXBContext jc = JAXBContext.newInstance(String.class, Bar.class);
    JAXBIntrospector introspector = jc.createJAXBIntrospector();
    Marshaller marshaller = jc.createMarshaller();
    if (null == introspector.getElementName(value)) {
        JAXBElement jaxbElement = new JAXBElement(new QName("ROOT"), Object.class, value);
        marshaller.marshal(jaxbElement, System.out);
    } else {/*from w w  w  .j a  v  a 2 s  .  co  m*/
        marshaller.marshal(value, System.out);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jxbc = JAXBContext.newInstance(Main.class);
    Marshaller marshaller = jxbc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.marshal(new JAXBElement(new QName("root"), Main.class, new Main("test")), System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Customer.class);

    Customer customer = new Customer();
    customer.setFirstName("Jane");
    customer.setLastName("Doe");

    PhoneNumber workPhone = new PhoneNumber();
    workPhone.setType("work");
    workPhone.setNumber("555-1111");
    customer.getPhoneNumbers().add(workPhone);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"), Customer.class,
            customer);/* w  w  w .  ja  v  a  2s .c  om*/
    marshaller.marshal(rootElement, System.out);
}

From source file:org.jasig.portlet.data.Exporter.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//from ww w .j  a  v  a  2s  .  c o  m
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);

    Object service = context.getBean(serviceBeanName);
    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    JAXBContext jc = JAXBContext.newInstance(modelClass);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Method method = service.getClass().getMethod(serviceBeanMethodName);
    List<?> objects = (List<?>) method.invoke(service, null);

    for (Object o : objects) {
        session.lock(o, LockMode.NONE);
        JAXBElement je2 = new JAXBElement(new QName(modelClass.getSimpleName().toLowerCase()), modelClass, o);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}

From source file:org.jasig.portlet.announcements.Exporter.java

public static void main(String[] args) throws Exception {

    String dir = args[0];//from w  w w.j a v  a 2s .c om
    ApplicationContext context = PortletApplicationContextLocator
            .getApplicationContext(PortletApplicationContextLocator.DATABASE_CONTEXT_LOCATION);
    SessionFactory sessionFactory = context.getBean(SESSION_FACTORY_BEAN_NAME, SessionFactory.class);
    IAnnouncementService announcementService = context.getBean(ANNOUNCEMENT_SVC_BEAN_NAME,
            IAnnouncementService.class);

    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    JAXBContext jc = JAXBContext.newInstance(Topic.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    List<Topic> topics = announcementService.getAllTopics();
    for (Topic topic : topics) {
        if (topic.getSubscriptionMethod() == 4) {
            continue;
        }

        session.lock(topic, LockMode.NONE);
        JAXBElement<Topic> je2 = new JAXBElement<Topic>(new QName("topic"), Topic.class, topic);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        System.out.println("Exporting Topic " + topic.getId() + " to file " + output);
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}

From source file:SyncLead.java

public static void main(String[] args) {
    System.out.println("Executing syncLead");
    try {/*  w  w  w  . j  a va2  s  .  co m*/
        URL marketoSoapEndPoint = new URL("https://100-AEK-913.mktoapi.com/soap/mktows/2_1" + "?WSDL");
        String marketoUserId = "demo17_1_809934544BFABAE58E5D27";
        String marketoSecretKey = "27272727aa";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsSyncLead request = new ParamsSyncLead();
        LeadRecord key = new LeadRecord();

        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<String> email = objectFactory.createLeadRecordEmail("george@jungle.com");
        key.setEmail(email);
        request.setLeadRecord(key);

        Attribute attr1 = new Attribute();
        attr1.setAttrName("FirstName");
        attr1.setAttrValue("George2");

        Attribute attr2 = new Attribute();
        attr2.setAttrName("LastName");
        attr2.setAttrValue("of the Jungle");

        ArrayOfAttribute aoa = new ArrayOfAttribute();
        aoa.getAttributes().add(attr1);
        aoa.getAttributes().add(attr2);

        QName qname = new QName("http://www.marketo.com/mktows/", "leadAttributeList");
        JAXBElement<ArrayOfAttribute> attrList = new JAXBElement(qname, ArrayOfAttribute.class, aoa);
        key.setLeadAttributeList(attrList);

        MktowsContextHeader headerContext = new MktowsContextHeader();
        headerContext.setTargetWorkspace("default");

        SuccessSyncLead result = port.syncLead(request, header, headerContext);

        JAXBContext context = JAXBContext.newInstance(SuccessSyncLead.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:SyncMultipleLeads.java

public static void main(String[] args) {
    System.out.println("Executing syncMultipleLeads");
    try {/*from www .  j  av  a2s .c  o  m*/
        URL marketoSoapEndPoint = new URL("https://100-AEK-913.mktoapi.com/soap/mktows/2_1" + "?WSDL");
        String marketoUserId = "demo17_1_809934544BFABAE58E5D27";
        String marketoSecretKey = "27272727aa";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsSyncMultipleLeads request = new ParamsSyncMultipleLeads();

        ObjectFactory objectFactory = new ObjectFactory();

        JAXBElement<Boolean> dedup = objectFactory.createParamsSyncMultipleLeadsDedupEnabled(true);
        request.setDedupEnabled(dedup);

        ArrayOfLeadRecord arrayOfLeadRecords = new ArrayOfLeadRecord();

        // Create First Lead Record
        LeadRecord rec1 = new LeadRecord();

        JAXBElement<String> email = objectFactory.createLeadRecordEmail("t@t.com");
        rec1.setEmail(email);

        Attribute attr1 = new Attribute();
        attr1.setAttrName("FirstName");
        attr1.setAttrValue("George");

        Attribute attr2 = new Attribute();
        attr2.setAttrName("LastName");
        attr2.setAttrValue("of the Jungle");

        ArrayOfAttribute aoa = new ArrayOfAttribute();
        aoa.getAttributes().add(attr1);
        aoa.getAttributes().add(attr2);

        QName qname = new QName("http://www.marketo.com/mktows/", "leadAttributeList");
        JAXBElement<ArrayOfAttribute> attrList = new JAXBElement(qname, ArrayOfAttribute.class, aoa);

        rec1.setLeadAttributeList(attrList);
        arrayOfLeadRecords.getLeadRecords().add(rec1);

        // Create Second Lead Record
        LeadRecord rec2 = new LeadRecord();

        JAXBElement<String> email2 = objectFactory.createLeadRecordEmail("myemail@test.com");
        rec2.setEmail(email2);

        Attribute attr11 = new Attribute();
        attr11.setAttrName("FirstName");
        attr11.setAttrValue("Nancy");

        Attribute attr21 = new Attribute();
        attr21.setAttrName("LastName");
        attr21.setAttrValue("Lady");

        ArrayOfAttribute aoa2 = new ArrayOfAttribute();
        aoa2.getAttributes().add(attr11);
        aoa2.getAttributes().add(attr21);

        qname = new QName("http://www.marketo.com/mktows/", "leadAttributeList");
        JAXBElement<ArrayOfAttribute> attrList2 = new JAXBElement(qname, ArrayOfAttribute.class, aoa2);

        rec2.setLeadAttributeList(attrList);
        arrayOfLeadRecords.getLeadRecords().add(rec2);

        request.setLeadRecordList(arrayOfLeadRecords);

        SuccessSyncMultipleLeads result = port.syncMultipleLeads(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessSyncMultipleLeads.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void parseBeanToXmlFileByJAXB(String path, Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);
    marshaller.marshal(rootElement, new FileOutputStream(path));
}

From source file:Main.java

public static String parseBeanToXmlStringByJAXB(Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);

    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        public void write(int b) throws IOException {
            this.string.append((char) b);
        }/* w w w .  j  a v  a  2s .co m*/

        //Netbeans IDE automatically overrides this toString()
        public String toString() {
            return this.string.toString();
        }
    };

    marshaller.marshal(rootElement, output);

    return output.toString();

}