一、引入Maven依赖
<dependency> <groupId>commons-betwixt</groupId> <artifactId>commons-betwixt</artifactId> <version>0.8</version> </dependency>
二、常用API说明
以下使用到一些BO:
Person.java
public class Person { private String username; private int age; private float stature;//身高 private boolean sex;//性别 private List list = new ArrayList(); private String[] friendsNames; private Map<String, String> maps = new HashMap<String, String>(); private Address address; public Person() { } public Person(String username) { this.username = username; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getStature() { return stature; } public void setStature(float stature) { this.stature = stature; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } @SuppressWarnings("unchecked") public List getList() { return list; } @SuppressWarnings("unchecked") public void setList(List list) { this.list = list; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public String[] getFriendsNames() { return friendsNames; } public void setFriendsNames(String[] friendsNames) { this.friendsNames = friendsNames; } @Override public String toString() { return "Person{" + "username=‘" + username + ‘\‘‘ + ", age=" + age + ", address=" + address + ‘}‘; } }
Address.java
public class Address { private String email; private List<String> telephone; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public List<String> getTelephone() { return telephone; } public void setTelephone(List<String> telephone) { this.telephone = telephone; } @Override public String toString() { return "Address{" + "email=‘" + email + ‘\‘‘ + ", telephone=" + telephone + ‘}‘; } }
常用API如下:
import java.io.StringReader; import java.io.StringWriter; import org.apache.commons.betwixt.io.BeanReader; import org.apache.commons.betwixt.io.BeanWriter; /** * Betwixt XML与Java对象之间相互转换. * 常用类: * ① BeanReader XML转Java对象 * ② BeanWriter Java对象转XML */ public class BetwixtDemo { private static final String XML_HEADER = "<?xml version=‘1.0‘ encoding=‘UTF-8‘?>\n"; private static void xmlToJavaBean() { try { // 先创建一个XML,在这里我们硬编码了一段XML内容 StringReader xmlReader = new StringReader(XML_HEADER + "<person><age>25</age><username>James Smith</username></person>"); //创建BeanReader BeanReader beanReader = new BeanReader(); //配置reader beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanReader.getBindingConfiguration().setMapIDs(false); //注册beans,以便betwixt知道XML将要被转化为一个什么Bean beanReader.registerBeanClass("person", Person.class); //现在我们对XML进行解析 Person person = (Person) beanReader.parse(xmlReader); //输出结果 System.out.println(person.toString()); } catch (Exception e) { System.out.println("xmlToJavaBean Error"); e.printStackTrace(); } } private static void javaBeanToXml() { try { // 先创建一个StringWriter,我们将把它写入为一个字符串 StringWriter outputWriter = new StringWriter(); // Betwixt在这里仅仅是将Bean写入为一个片断 // 所以如果要想完整的XML内容,我们应该写入头格式 outputWriter.write(XML_HEADER); // 创建一个BeanWriter,其将写入到我们预备的stream中 BeanWriter beanWriter = new BeanWriter(outputWriter); // 配置betwixt // 更多详情请参考java docs 或最新的文档 beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanWriter.getBindingConfiguration().setMapIDs(false); beanWriter.enablePrettyPrint(); // 如果这个地方不传入XML的根节点名,Betwixt将自己猜测是什么 // 但是让我们将例子Bean名作为根节点吧 Person person = new Person(); person.setUsername("张三"); person.setAge(25); // 默认以类名为标签,也可以指定 //beanWriter.write(person); beanWriter.write("person", person); //输出结果 System.out.println(outputWriter.toString()); // Betwixt写的是片断而不是一个文档,所以不要自动的关闭掉writers或者streams, //但这里仅仅是一个例子,不会做更多事情,所以可以关掉 outputWriter.close(); } catch (Exception e) { System.out.println("javaBeanToXml Error"); e.printStackTrace(); } } public static void main(String[] args) { //BetwixtDemo.xmlToJavaBean(); BetwixtDemo.javaBeanToXml(); } }