【问题标题】:Java to xml Serialization: How can i get fields name as a tag?Java 到 xml 序列化:如何将字段名称作为标签?
【发布时间】:2015-09-02 14:25:59
【问题描述】:

MySql 查询:

select 'customers'  tableName , customers.* from customers

这是序列化函数:

        List<?> received = exchange.getIn().getBody(List.class);
        FileOutputStream fich   = new FileOutputStream("xmlop.xml");
            XMLEncoder x = new XMLEncoder(new BufferedOutputStream(fich));
            x.writeObject(received);
            x.flush();
            x.close();

结果

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_45" class="java.beans.XMLDecoder">
 <object class="java.util.ArrayList">
  <void method="add">
   <object class="org.springframework.util.LinkedCaseInsensitiveMap">
    <void method="put">
     <string>tableName</string>
     <string>customers</string>
    </void>
    <void method="put">
     <string>id</string>
     <int>1</int>
    </void>
    <void method="put">
     <string>firstname</string>
     <string>zied</string>
    </void>
    <void method="put">
     <string>lastname</string>
     <string>saffar</string>
    </void>
   </object>
  </void>
  <void method="add">
   <object class="org.springframework.util.LinkedCaseInsensitiveMap">
    <void method="put">
     <string>tableName</string>
     <string>customers</string>
    </void>
    <void method="put">
     <string>id</string>
     <int>2</int>
    </void>
    <void method="put">
     <string>firstname</string>
     <string>mohamed</string>
    </void>
    <void method="put">
     <string>lastname</string>
     <string>houidi</string>
    </void>
   </object>
  </void>
 </object>
</java>

我该怎么做才能有这样的东西:

<customer>
    <id>1</id>
    <firstname>zied</firstname>
    <lastname>saffar</lastname>
</customer>
<customer>
    <id>1</id>
    <firstname>zied</firstname>
    <lastname>saffar</lastname>
</customer>

=> 名称字段作为标签

【问题讨论】:

    标签: java xml xml-serialization


    【解决方案1】:

    您可能希望使用 JAXB 来实现。你还需要创建一个特殊的类。

    在这里查看:http://www.mkyong.com/java/jaxb-hello-world-example/

    【讨论】:

    • 在我的情况下,我没有与 jaxb 一起使用的类,我有一个 List 作为 sql 查询结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多