对象转化为Xml
???? 内部系统与外部系统对接时,需要进行数据交换,一般是采用XML格式。目前的OXM已基本满足要求,但是对于更加的复杂XMl数据,仍需要手工编写。
???? 由于是手工编写ORM,编写风格、方式因人而异。我也尝试写一个,同时和大家交流心得。
?
??? 代码说明:
??? 1、两个类Person和Address,一个Peson有多个Address
??? 2、使用Dom4j
??? 实现要求:
??? 1、可指定xml的文件名
??? 2、每个xml文件的person记录数不超过指定数量
?/** * 地址 */public class Address { private String city; private String country; private String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; }}?
/** * 人 */public class Person { private String name; private int age; private int height; private List<Address> addresses = new ArrayList<Address>(3); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public List<Address> getAddresses() { return addresses; } public void addAddress(Address address){ addresses.add(address); }}
?
?public class XmlPerson { private Person person; public XmlPerson() {} public void setPerson(Person person){ this.person = person; } public Element toNameElement(){ CDATA c = DocumentHelper.createCDATA("name"); c.setText(person.getName()); Element e = DocumentHelper.createElement("name"); e.add(c); ?return e; } public Element toHeightElement(){ Element e = DocumentHelper.createElement("height"); e.setText(""+person.getHeight()); return e; } public Element toAgeElement(){ Element e = DocumentHelper.createElement("age"); e.setText(""+person.getAge()); return e; } private Element toAddressElement(Address address){ Element addr = DocumentHelper.createElement("address"); Element city = DocumentHelper.createElement("city"); city.setText(address.getCity()); Element country = DocumentHelper.createElement("country"); country.setText(address.getCountry()); addr.add(country); addr.add(city); return addr; } public Element toAddressesElements(){ Element addressList = DocumentHelper.createElement("addressList"); for(Address addr:person.getAddresses()){ addressList.add(toAddressElement(addr)); } return addressList; } public Element toPersonElement(){ Element e = DocumentHelper.createElement("person"); e.add(toNameElement()); e.add(toHeightElement()); e.add(toAgeElement()); e.add(toAddressesElements()); return e ; } public static Element toPersonElement(List<Person> persons){ XmlPerson xp =new XmlPerson(); Element personList = DocumentHelper.createElement("persons"); for(Person p:persons){ xp.setPerson(p); personList.add(xp.toPersonElement()); } return personList; } public static void writePersonToXmlFile(String fileName,List<Person> persons ) throws IOException { Document doc = DocumentHelper.createDocument(); doc.add(XmlPerson.toPersonElement(persons)); writeDocumentToXmlFile(fileName, doc); } public static void writeDocumentToXmlFile(String fileName,Document doc) throws IOException { File file = new File(fileName) ; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter xr = new XMLWriter(new FileOutputStream(file),format); xr.write(doc); xr.flush(); } public static void writePersonToXmlFile(String preName,List<Person> persons,int size) throws IOException { for(int i = 0 ; i< persons.size() ;i+=size){ int start = i; int end = i+size; if(start > persons.size()){ start = persons.size()-1; end = persons.size() ; } if(end >persons.size()){ end = persons.size() ; } String name = preName+start+"."+"xml"; writePersonToXmlFile(name,persons.subList(start,end)); } }}
?
?public class XmlPersonTest { Document doc; List<Person> ps ; @Before public void setUp(){ ps = new ArrayList<Person>(100); for(int i = 0 ; i< 100; i++){ Person p =new Person(); p.setAge(i); p.setHeight(i); p.setName("ming"+i); Address addr1 = new Address(); addr1.setCity("jinjiang"+i); addr1.setCountry("china"); Address addr2 = new Address(); addr2.setCity("shenzhen"+i); addr2.setCountry("china"); p.addAddress(addr2); ps.add(p); } doc = DocumentHelper.createDocument(); } @After public void tearDown(){ ps= null; doc = null; } @Test public void testWriteXmlPerson() throws IOException { XmlPerson.writePersonToXmlFile("person",ps,13); }}?
?
??? 心得说明:
??? 1、使用组合的方式来处理Person。一方面不入侵person,另一方面转化逻辑集中
??? 2、在解析Xml的过程中,需要对属性进行归类,把同一种概念的属性,放到统一的方法里进行处理。
??? 3、使用测试开发,一小步一小步前进。
?
??? 其他:
??? 在数据格式转化过程中,关键是 建立一群相关对象,然后根据这些对象转化成其他格式。
??? 比如在网址支付结果的处理过程,一方面需要把支付数据传到页面,另一方面以Xml格式传给财务系统。我的解决思路是建立一个Payment类,然后把所需的信息整合到payment中。如果要传递到页面,生成方法CreatePaymentVO,如果要传给财务,构造XMLPaymnt类。虽然类有点多,但是可维护性强。
?
?