1

I have a group of working code to construct an XML Request in certain specific format as per below. But the logic was just hardcoded example emaillist.appendChild document.createElement and so on. Is there any better way of achieving the same result? Maybe using some Stub or Custom XML type Java Entity?

public void setXML() throws JAXBException, ParserConfigurationException {

 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 Document document = docBuilder.newDocument();
 Element emaillist = document.createElement("EMAILLIST");

 Element templateID = document.createElement("TemplateID");
 templateID.setTextContent("1");

 Element campaignID = document.createElement("CampaignID");
 campaignID.setTextContent("100");

 Element campaignName = document.createElement("CampaignName");
 campaignName.setTextContent("Test Campaign Using XML DS");

 Element category = document.createElement("Category");
 category.setTextContent("Trigger");

 Element email = document.createElement("Email");
 email.setTextContent("test@test.com");

 Element subject = document.createElement("Subject");
 subject.setTextContent("Transactional Submissions");

 Element senderEmail = document.createElement("SenderEmail");
 senderEmail.setTextContent("support@test");

 Element emailBody = document.createElement("EmailBody");
 Element html = document.createElement("html");
 Element head = document.createElement("head");
 Element title = document.createElement("title");
 title.setTextContent("This is test Title : " + ZonedDateTime.now());
 head.appendChild(title);
 Element body = document.createElement("body");
 html.appendChild(head);
 html.appendChild(body);
 emailBody.appendChild(html);

 emaillist.appendChild(templateID);
 emaillist.appendChild(campaignID);
 emaillist.appendChild(campaignName);
 emaillist.appendChild(category);
 emaillist.appendChild(email);
 emaillist.appendChild(subject);
 emaillist.appendChild(senderEmail);
 emaillist.appendChild(emailBody);
 System.out.println(emaillist);

}

Sample Required Output

<EMAILLIST
    xmlns:ns2="TransactionalSubmissionsSvcs"
    xmlns="">
    <TemplateID>1</TemplateID>
    <CampaignID>100</CampaignID>
    <CampaignName>Test Campaign Using XML DS</CampaignName>
    <Category>Trigger</Category>
    <Email>test@test.com.my</Email>
    <Subject>Transactional Submissions</Subject>
    <SenderEmail>test@test.com.my</SenderEmail>
    <EmailBody>
        <html>
            <head>
                <title>This is test Title : 2019-03-17T20:32:43.649+08:00</title>
            </head>
            <body/>
        </html>
    </EmailBody>
</EMAILLIST>
XuSen
  • 35
  • 6
  • See https://stackoverflow.com/questions/23520208/how-to-create-xml-file-with-specific-structure-in-java – Progman Mar 17 '19 at 16:46
  • The post suggested is as per what i have did. But i’m trying to find a better way to achieve this because i believe my solution now is bad. – XuSen Mar 17 '19 at 18:36
  • As mentioned in the other questions, you can use JAXB, which will fill you objects automatically with the values. – Progman Mar 17 '19 at 18:43

1 Answers1

1

Create POJO model which represents your XML and after that serialiser it to XML using JAXB, Jackson XML or other library. Example with POJO model and JAXB:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.time.ZonedDateTime;

public class JaxbApp {

    public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(EmailList.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        EmailBody emailBody = new EmailBody();
        emailBody.setTitle("This is test Title : " + ZonedDateTime.now());
        emailBody.setBody("");

        EmailList emailList = new EmailList();
        emailList.setTemplateId(1);
        emailList.setCampaignId(100);
        emailList.setCampaignName("Test Campaign Using XML DS");
        emailList.setCategory("Trigger");
        emailList.setEmail("test@test.com.my");
        emailList.setSubject("Transactional Submissions");
        emailList.setSenderEmail("test@test.com.my");
        emailList.setEmailBody(emailBody);

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "EMAILLIST", namespace = "TransactionalSubmissionsSvcs")
class EmailList {

    @XmlElement(name = "TemplateID")
    private int templateId;

    @XmlElement(name = "CampaignID")
    private int campaignId;

    @XmlElement(name = "CampaignName")
    private String campaignName;

    @XmlElement(name = "Category")
    private String category;

    @XmlElement(name = "Email")
    private String email;

    @XmlElement(name = "Subject")
    private String subject;

    @XmlElement(name = "SenderEmail")
    private String senderEmail;

    @XmlElement(name = "EmailBody")
    private EmailBody emailBody;

    // getters, setters
}

class EmailBody {

    private String title;
    private String body;

    // getters, setters
}

Above code prints:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:EMAILLIST xmlns:ns2="TransactionalSubmissionsSvcs">
    <TemplateID>1</TemplateID>
    <CampaignID>100</CampaignID>
    <CampaignName>Test Campaign Using XML DS</CampaignName>
    <Category>Trigger</Category>
    <Email>test@test.com.my</Email>
    <Subject>Transactional Submissions</Subject>
    <SenderEmail>test@test.com.my</SenderEmail>
    <EmailBody>
        <body></body>
        <title>This is test Title : 2019-03-18T00:03:48.168+01:00</title>
    </EmailBody>
</ns2:EMAILLIST>

If you really need html and head elements you need to create simple POJO wrappers.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146