简体   繁体   English

java xmlencoder格式不正确

[英]java xmlencoder incorrect format

xml encoding problem xml编码问题

java xmlencoder is output incorrectly. java xmlencoder 输出错误。 Below I gave the expected output as an image.下面我给出了预期的输出作为图像。 How can I get this output with xmlencoder or is there a different way?如何使用 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();
}

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:预期输出:

在此处输入图片说明 how else can I do this?我还能怎么做?

Use JAXB (Java Architecture for XML Binding) library for marshaling java objexts to XML使用JAXB(用于 XML 绑定的 Java 架构)库将 java objexts 编组到 XML

Here's an example https://dzone.com/articles/using-jaxb-for-xml-with-java这是一个示例https://dzone.com/articles/using-jaxb-for-xml-with-java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM