由于xml的扩展性比较好,有些项目经常用到Xml和和对象互转,常常遇到Xml字符串拼错、以及一些括号需要转码的问题, 目前开源的xml 转obj 库使用起来极为不方便,效率也比较低,例如SimpleXml,和betwixt,下面给大家介绍一款新的xml和java互转的库,经测试,解析或者生成一个2000行左右的xml字符串文本,耗时是SimpleXml的1/5左右,下载地址https://github.com/ludaiqian/appliedxml
几个小Demo:
1、对javabean的序列化
public class BeanTest { // 该字段将不会被序列化 @Transient private String version; // 属性 @Attribute private String attr1; private String element1; private String element2; private String[] testKeys = { "a", "b", "c" }; private JavaBean javabean; public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getAttr1() { return attr1; } public void setAttr1(String attr1) { this.attr1 = attr1; } public String getElement1() { return element1; } public void setElement1(String element1) { this.element1 = element1; } public String getElement2() { return element2; } public void setElement2(String element2) { this.element2 = element2; } public String[] getTestKeys() { return testKeys; } public void setTestKeys(String[] testKeys) { this.testKeys = testKeys; } public JavaBean getJavabean() { return javabean; } public void setJavabean(JavaBean javabean) { this.javabean = javabean; } public BeanTest() { } public static void main(String[] args) throws IOException { // 创建序列化转换对象 Serializer serializer = new Serializer(); // 设置是否格式化 serializer.setFormatted(true); // 空置是否序列化 serializer.setNullValueSerializeRequired(false); BeanTest beanTest = new BeanTest(); beanTest.setVersion("1.0"); beanTest.setAttr1("attr"); beanTest.setElement1("e1"); beanTest.setElement2("e2"); JavaBean javabean = new JavaBean(); javabean.setBean1("b1"); beanTest.setJavabean(javabean); // 将对象转为xml String xml = serializer.toXml(beanTest); System.out.println(xml); } public static class JavaBean { String bean1; String bean2; public String getBean1() { return bean1; } public void setBean1(String bean1) { this.bean1 = bean1; } public String getBean2() { return bean2; } public void setBean2(String bean2) { this.bean2 = bean2; } @Override public String toString() { return "JavaBean [bean1=" + bean1 + ", bean2=" + bean2 + "]"; } } }
运行结果:
<beanTest attr1="attr">
<element1>e1</element1>
<element2>e2</element2>
<testKeys>
<string>a</string>
<string>b</string>
<string>c</string>
</testKeys>
<javabean>
<bean1>b1</bean1>
</javabean>
</beanTest>
2、List序列化:
public class ListTest { @ElementList(name = "items", entry = "item") private ArrayList<String> arrayList = new ArrayList<String>(); public ListTest() { arrayList.add("test1"); arrayList.add("test2"); arrayList.add("test3"); arrayList.add("test4"); arrayList.add("test5"); } public static void main(String[] args) throws IOException { Serializer serializer = new Serializer(); serializer.setFormatted(true); System.out.println(serializer.toXml(new ListTest())); } }
<listTest>
<items>
<item>test1</item>
<item>test2</item>
<item>test3</item>
<item>test4</item>
<item>test5</item>
</items>
</listTest>
3、Map 测试:
public class MapTest { @ElementMap(entry = "hello", keyAsAttribute = true, valueAsText = true) private HashMap<String, String> hellos = new HashMap<String, String>(); @ElementMap(entry = "word") private HashMap<String, String> words = new HashMap<String, String>(); public MapTest() { hellos.put("attr1", "text1"); hellos.put("attr2", "text2"); hellos.put("attr3", "text3"); hellos.put("attr4", "text4"); // words.put("k1", "v1"); words.put("k2", "v2"); words.put("k3", "v3"); words.put("k4", "v4"); } public static void main(String[] args) throws IOException { Serializer serializer = new Serializer(); serializer.setFormatted(true); System.out.println(serializer.toXml(new MapTest())); } }
<mapTest>
<hellos>
<hello key="attr2">text2</hello>
<hello key="attr1">text1</hello>
<hello key="attr4">text4</hello>
<hello key="attr3">text3</hello>
</hellos>
<words>
<word>
<key>k3</key>
<value>v3</value>
</word>
<word>
<key>k4</key>
<value>v4</value>
</word>
<word>
<key>k1</key>
<value>v1</value>
</word>
<word>
<key>k2</key>
<value>v2</value>
</word>
</words>
</mapTest>