XML header is not shown

Peter Penzov :

I'm trying to send XML request. I tried this code:

        PaymentTransaction pt = new PaymentTransaction();
        ........

        JAXBContext jaxbContext = JAXBContext.newInstance(PaymentTransaction.class);

        // Create Marshaller
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        // Print XML String to Console
        StringWriter sws = new StringWriter();
        jaxbMarshaller.marshal(pt, sws);

        // Verify XML Content
        String xmlContent = sws.toString();

        Mono<PaymentResponse> result = client.executeAndReceive(xmlContent);

I tried to set manually the XML header <?xml version="1.0" encoding="UTF-8" standalone="yes"?> but it's not displayed when I print the result. Do you know why?

Vishwa Ratna :

I have tried the same on My own PC and should definitely work for you:

    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.util.List;
    @XmlRootElement(name = "myOrg")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class MyOrganisation {
        @XmlElement(name = "departmentName")
        public String departmentName;
        @XmlElement(name = "employeeID")
        public int employeeID;
        @XmlElement(name = "employeeName")
        public String employeeName;
        @XmlElement(name = "tasks")
        public List<String> tasks;

        public MyOrganisation() {
        }

        public MyOrganisation(String departmentName, int employeeID, String employeeName, List<String> tasks) {
            this.departmentName = departmentName;
            this.employeeID = employeeID;
            this.employeeName = employeeName;
            this.tasks = tasks;
        }

        public String getDepartmentName() {
            return departmentName;
        }

        public void setDepartmentName(String departmentName) {
            this.departmentName = departmentName;
        }

        public int getEmployeeID() {
            return employeeID;
        }

        public void setEmployeeID(int employeeID) {
            this.employeeID = employeeID;
        }

        public String getEmployeeName() {
            return employeeName;
        }

        public void setEmployeeName(String employeeName) {
            this.employeeName = employeeName;
        }

        public List<String> getTasks() {
            return tasks;
        }

        public void setTasks(List<String> tasks) {
            this.tasks = tasks;
        }
    }
_______

My main() Class:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.Arrays;
public class JAXBxml {

    public static void main(String[] args) {
       MyOrganisation myOrg = new MyOrganisation();
        try {
            JAXBContext context = JAXBContext.newInstance(myOrg.getClass());
            StringWriter writer = new StringWriter();
            writer.append("<?xml version=\"1.0\"?>");
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

            myOrg.setDepartmentName("Cognizant");
            myOrg.setEmployeeID(140012);
            myOrg.setDepartmentName("Vishwa Ratna");
            myOrg.setTasks(Arrays.asList("Coding","Deployment","Production"));

            marshaller.marshal(myOrg, writer);
            String stringXML = writer.toString();
            System.out.println(stringXML);

        } catch (JAXBException e) {
            throw new RuntimeException("Problems generating XML in specified "
                    + "encoding, underlying problem is " + e.getMessage(),
                    e);
        }
    }
}

Output:

<?xml version="1.0"?>
<myOrg>
    <departmentName>Vishwa Ratna</departmentName>
    <employeeID>140012</employeeID>
    <tasks>Coding</tasks>
    <tasks>Deployment</tasks>
    <tasks>Production</tasks>
</myOrg>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308832&siteId=1
Recommended