Yusuf Koçak
Yusuf Koçak

Reputation: 119

java xmlencoder incorrect format

xml encoding problem

java xmlencoder is output incorrectly. Below I gave the expected output as an image. How can I get this output with xmlencoder or is there a different way?

public static String serializeToXML(Object obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(baos);
    encoder.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception e) {
            System.out.println("Exception! :" + e.toString());
        }
    });
    encoder.writeObject(obj);
    encoder.close();
    baos.close();

    return baos.toString();
}

output:

<object class="vx.app.trkcll.client.Settlement">
  <void property="password">
  <string>62622</string>
   </void>
   <void property="taxNumber">
   <string>8899001122</string>
   </void>
    <void property="transactionDate">
    <string>2018-12-21 16:12:31</string>
   </void>
   <void property="userName">
   <string>5350008998</string>
  </void>
  </object>

expected output:

enter image description here how else can I do this?

Upvotes: 1

Views: 99

Answers (1)

Sameera Manorathna
Sameera Manorathna

Reputation: 618

Use JAXB (Java Architecture for XML Binding) library for marshaling java objexts to XML

Here's an example https://dzone.com/articles/using-jaxb-for-xml-with-java

Upvotes: 1

Related Questions