java xmlencoder格式错误

时间:2018-12-21 13:49:56

标签: java xml

xml编码问题

java xmlencoder输出错误。下面我给出了预期的输出图像。 如何使用xmlencoder获得此输出,或者有其他方法吗?

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();
}

输出:

<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>

预期输出:

enter image description here 我还能怎么做?

1 个答案:

答案 0 :(得分:1)

使用 JAXB(用于XML绑定的Java体系结构)库将Java objexts编组为XML

下面是一个示例https://dzone.com/articles/using-jaxb-for-xml-with-java

相关问题