Java到xml序列化:如何将字段名称作为标记?

时间:2015-09-02 14:26:00

标签: java xml xml-serialization

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>

=&GT;将字段名称作为标记&lt; ==

1 个答案:

答案 0 :(得分:0)

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

点击此处:http://www.mkyong.com/java/jaxb-hello-world-example/

相关问题