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:Main.java

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

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

    IntegerChild integerChild = new IntegerChild();
    integerChild.getItem().add(1);//from  w w  w.j  a v  a 2s . c om
    integerChild.getItem().add(2);
    marshaller.marshal(integerChild, System.out);

    StringChild stringChild = new StringChild();
    stringChild.getItem().add("A");
    stringChild.getItem().add("B");
    marshaller.marshal(stringChild, System.out);
}

From source file:MarshalValidation.java

public static void main(String[] args) throws Exception {
    Person p = new Person();
    p.setFirstName("B");
    p.setLastName("H");

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("person.xsd"));

    marshaller.setSchema(schema);/*from   ww w.  ja va 2s.  co  m*/
    marshaller.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event);
            return false;
        }
    });

    marshaller.marshal(p, System.out);
}

From source file:GetLead.java

public static void main(String[] args) {
    System.out.println("Executing Get Lead");
    try {/*from  w w w .j av  a 2 s .  c  o  m*/

        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        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
        ParamsGetLead request = new ParamsGetLead();

        LeadKey key = new LeadKey();
        key.setKeyType(LeadKeyRef.EMAIL);
        key.setKeyValue("t@t.com");
        request.setLeadKey(key);

        SuccessGetLead result = port.getLead(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLead.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 main(String[] args) throws Exception {
    Main example = new Main();
    example.data.put("France", "Paris");
    example.data.put("Japan", "Tokyo");

    JAXBContext context = JAXBContext.newInstance(Main.class);
    Marshaller marshaller = context.createMarshaller();
    DOMResult result = new DOMResult();
    marshaller.marshal(example, result);

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    Document document = (Document) result.getNode();
    XPathExpression expression = xpath.compile("//map/entry");
    NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET);

    expression = xpath.compile("//map");
    Node oldMap = (Node) expression.evaluate(document, XPathConstants.NODE);
    Element newMap = document.createElement("map");

    for (int index = 0; index < nodes.getLength(); index++) {
        Element element = (Element) nodes.item(index);
        newMap.setAttribute(element.getAttribute("key"), element.getAttribute("value"));
    }/*from   w  w  w .  j a  va 2s.  c  om*/

    expression = xpath.compile("//map/..");
    Node parent = (Node) expression.evaluate(document, XPathConstants.NODE);
    parent.replaceChild(newMap, oldMap);

    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),
            new StreamResult(System.out));
}

From source file:GetImportToListStatus.java

public static void main(String[] args) {
    System.out.println("Executing Get Import To List Status");
    try {// ww  w .j  av a2s . c o  m
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        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
        ParamsGetImportToListStatus request = new ParamsGetImportToListStatus();

        request.setProgramName("Trav-Demo-Program");
        request.setListName("Trav-Test-List");

        SuccessGetImportToListStatus result = port.getImportToListStatus(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetImportToListStatus.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:GetChannels.java

public static void main(String[] args) {
    System.out.println("Executing Get Channels");
    try {/*w w  w  .j  av  a  2  s.  co m*/
        URL marketoSoapEndPoint = new URL("CHANGEME" + "?WSDL");
        String marketoUserId = "CHANGEME";
        String marketoSecretKey = "CHANGEME";

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

        // 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
        ParamsGetChannels params = new ParamsGetChannels();
        Tag tags = new Tag();
        ArrayOfString tagArray = new ArrayOfString();
        tagArray.getStringItems().add("Webinar");
        tagArray.getStringItems().add("Blog");
        tagArray.getStringItems().add("Tradeshow");
        tags.setValues(tagArray);

        MktowsPort port = service.getMktowsApiSoapPort();
        SuccessGetChannels result = port.getChannels(params, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLead.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 main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Disk.class, MyStatus.class, MyDisk.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    Disk disk = new Disk();
    disk.setStatus("attached");
    disk.setSize(10000000000L);//from   w ww. j a  v  a 2s  .c o  m
    disk.setFreeSpace(25600000L);
    disk.setId("1");

    m.marshal(disk, System.out);
    m.marshal(new MyStatus(disk), System.out);
    m.marshal(new MyDisk(disk), System.out);
}

From source file:GetTags.java

public static void main(String[] args) {

    System.out.println("Executing Get Tags");
    try {//from  w w w.  j  a  v a  2 s  . 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
        ParamsGetTags request = new ParamsGetTags();
        ArrayOfTag tags = new ArrayOfTag();

        Tag tag = new Tag();
        tag.setType("Content Channel");
        /*
        ArrayOfString values = new ArrayOfString();
        values.getStringItems().add("SEM");
        values.getStringItems().add("Email");
                 
        tag.setValues(values);
        tags.getTags().add(tag);
                 
        request.setTagList(tags);
        */

        SuccessGetTags result = port.getTags(request, header);
        JAXBContext context = JAXBContext.newInstance(SuccessGetTags.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:GetCustomObjects.java

public static void main(String[] args) {
    System.out.println("Executing Get Custom Objects");
    try {//from   w ww .j  a va 2  s.  co m
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        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
        ParamsGetCustomObjects request = new ParamsGetCustomObjects();
        request.setObjTypeName("RoadShow");

        ArrayOfAttribute arrayOfAttribute = new ArrayOfAttribute();

        Attribute attr = new Attribute();
        attr.setAttrName("MKTOID");
        attr.setAttrValue("1090177");
        arrayOfAttribute.getAttributes().add(attr);

        JAXBElement<ArrayOfAttribute> attributes = new ObjectFactory()
                .createParamsGetCustomObjectsCustomObjKeyList(arrayOfAttribute);
        request.setCustomObjKeyList(attributes);

        SuccessGetCustomObjects result = port.getCustomObjects(request, header);
        JAXBContext context = JAXBContext.newInstance(SuccessGetCustomObjects.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:GetCampaignsForSource.java

public static void main(String[] args) {
    System.out.println("Executing Get Campaigns For Source");
    try {//w ww  . jav a  2 s  . c o m
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        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
        ParamsGetCampaignsForSource request = new ParamsGetCampaignsForSource();

        request.setSource(ReqCampSourceType.MKTOWS);

        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<String> name = objectFactory.createParamsGetCampaignsForSourceName("Trigger");
        request.setName(name);

        JAXBElement<Boolean> exactName = objectFactory.createParamsGetCampaignsForSourceExactName(false);
        request.setExactName(exactName);

        SuccessGetCampaignsForSource result = port.getCampaignsForSource(request, header);

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

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