java 简单的 XML 反序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1910694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple XML deserialization
提问by Binil Thomas
I am trying out the Simple XML serializer. I am more interested in deserialization from XML->Java. Here is my code as a unit test:
我正在尝试Simple XML serializer。我对 XML->Java 的反序列化更感兴趣。这是我作为单元测试的代码:
import java.io.StringReader;
import java.io.StringWriter;
import junit.framework.TestCase;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class SimpleTest extends TestCase {
public void testWriting() throws Exception {
StringWriter writer = new StringWriter();
Address address = new Address("1234 Main Street", "San Francisco", "CA");
Serializer serializer = new Persister();
serializer.write(address, writer);
System.out.println("Wrote: " + writer.getBuffer());
}
public void testReading() throws Exception {
String input = "<address street='1234 Main Street' city='San Francisco' state='CA'/>";
Serializer serializer = new Persister();
System.out.println("Read back: " + serializer.read(Address.class, new StringReader(input)));
}
}
@Root
class Address {
@Attribute(name="street")
private final String street;
@Attribute(name="city")
private final String city;
@Attribute(name="state")
private final String state;
public Address(@Attribute(name="street") String street, @Attribute(name="city") String city, @Attribute(name="state") String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}
@Override
public String toString() {
return "Address [city=" + city + ", state=" + state + ", street=" + street + "]";
}
}
This works, but the repeated @Attributeannotations (at the field and at the constructor argument) in the Address class look ugly. Is there some way to:
这是有效的,但@AttributeAddress 类中的重复注释(在字段和构造函数参数处)看起来很难看。有没有办法:
- have simple figure out the attribute name from the field name?
- have simple ignore serialization, so that I can get away with annotating either the fields or the constructor argument?
- 有简单的从字段名中找出属性名吗?
- 有简单的忽略序列化,以便我可以摆脱注释字段或构造函数参数?
回答by DJ.
I don't think you need all that repetition and the extra annotations' attribute. If the name is the same as the object attribute, it will be used by default.
我认为您不需要所有的重复和额外的注释属性。如果名称与对象属性相同,则默认使用。
so you can just declare it as:
所以你可以将它声明为:
@Root
class Address {
@Attribute
private final String street;
@Attribute
private final String city;
@Attribute
private final String state;
public Address(String street, String city, String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}
@Override
public String toString() {
return "Address [city=" + city + ", state=" + state + ", street=" + street + "]";
}
}
回答by spork
Java will serialize all of your class members by default if they implement Serializableand adhere to the JavaBean syntax.
如果 Java 实现Serializable并遵守 JavaBean 语法,则默认情况下 Java 将序列化所有类成员。
回答by DiSol
I had similar concern but with pretty complicated objects structures. I solved it by using JAXB library for marshaling and de-marshaling which is a pretty common one. You can consider also totally separating the marshaling logic from your java class by using aspects - you can treat all the annotations in a separate aspect what will make your java class fully clean from marshaling annotations.
我有类似的担忧,但对象结构非常复杂。我通过使用 JAXB 库进行编组和解组来解决它,这是一种非常常见的方法。您还可以考虑通过使用方面将封送处理逻辑与您的 Java 类完全分离 - 您可以在一个单独的方面处理所有注释,这将使您的 Java 类完全从封送处理注释中清除。

