Jaxb實現 複雜POJO 與 xml 的轉換

來源:互聯網
上載者:User

jaxml各參數請看:http://blog.csdn.net/z69183787/article/details/12970523

Person:

package com.test;import java.util.Date;import java.util.List;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlTransient;import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;@XmlRootElement(name = "person")public class Person {@XmlElement(name = "name")private String name ;@XmlElement(name = "age")private String age;@XmlElement(name = "BasicData")private List<Child> children;private Date d;@XmlElement(name = "time")@XmlJavaTypeAdapter(JaxbDateSerializer.class) public Date getD() {return d;}public void setD(Date d) {this.d = d;}@XmlTransientpublic List<Child> getChildren() {return children;}public void setChildren(List<Child> children) {this.children = children;}@XmlTransientpublic String getName() {return name;}public void setName(String name) {this.name = name;}@XmlTransientpublic String getAge() {return age;}public void setAge(String age) {this.age = age;}}


Child.java

package com.test;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlAccessType;@XmlRootElement(name="BasicData")@XmlAccessorType(XmlAccessType.FIELD)public class Child {private String sex;public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}


JaxbDateSerializer.java 處理Date

package com.test;import java.text.SimpleDateFormat;import java.util.Date;import javax.xml.bind.annotation.adapters.XmlAdapter;public class JaxbDateSerializer extends XmlAdapter<String, Date>{    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    @Override     public String marshal(Date date) throws Exception {         return dateFormat.format(date);     }    @Override     public Date unmarshal(String date) throws Exception {         return dateFormat.parse(date);     } }


Main

package com.test;import java.io.FileWriter;import java.io.IOException;import java.io.StringWriter;import java.net.URL;import java.util.Date;import java.util.ArrayList;import java.util.List;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;public class Test {public static void main(String[] args) throws JAXBException, IOException{System.out.println(java.net.URLEncoder.encode("的","utf-8"));//java 類 轉 xml 檔案List<Child> l = new ArrayList<Child>();Person p = new Person();p.setAge("123");p.setName("周順");p.setD(new Date());for(int i=0;i<5;i++){Child c = new Child();c.setSex(i+"");l.add(c);}p.setChildren(l);StringWriter writer = new StringWriter();URL read = ClassLoader.getSystemResource("com/test/person.xml");FileWriter fw = new FileWriter(read.getPath());JAXBContext context = JAXBContext.newInstance(Person.class);Marshaller m = context.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_ENCODING, "GBK"); //防止檔案中文亂碼m.marshal(p, writer);m.marshal(p, fw);fw.flush();fw.close();String xml = writer.toString();System.out.println(xml);//xml檔案轉java類 JAXBContext context2 = JAXBContext.newInstance(Person.class);        Unmarshaller unmarshaller = context2.createUnmarshaller();        //class目錄 jaxb/bin        //URL xmlFileUrl = ClassLoader.getSystemResource("");        URL xmlFileUrl = ClassLoader.getSystemResource("com/test/person.xml");        System.out.println(xmlFileUrl.getPath());        Person p2 = (Person) unmarshaller.unmarshal(xmlFileUrl);               //  SAXReader reader = new SAXReader();             //  Document document = reader.read(new File(           //Test.class.getClassLoader().getResource("com/test/person.xml").getPath()));       // Person p2 = (Person) unmarshaller.unmarshal(new StringReader(document.asXML()));        System.out.println(p2.getName());        System.out.println(p2.getChildren().get(4).getSex());}}



xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><person><name>zhoushun</name><age>123</age><BasicData><sex>0</sex></BasicData><BasicData><sex>1</sex></BasicData><BasicData><sex>2</sex></BasicData><BasicData><sex>3</sex></BasicData><BasicData><sex>4</sex></BasicData><time>2013-10-31</time></person>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.