实体类:
package org.senssic.jaxrs.bean; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User { private String name; private int age; private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } 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; } @Override public String toString() { return "名字:" + name + "年龄:" + age + "时间:" + date; } }
转换:
package org.senssic.jaxrs.test; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import org.senssic.jaxrs.bean.User; public class Test { public static void main(String[] args) throws Exception { // 实体转化为xml // User u = new User(); // u.setName("aaaaa"); // u.setAge(20); // u.setDate(new Date()); // JAXBContext jContext = JAXBContext.newInstance(User.class); // Marshaller marshaller = jContext.createMarshaller(); // marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true); // marshaller.marshal(u,System.out); // xml转化为实体 JAXBContext jContext = JAXBContext.newInstance(User.class); Unmarshaller unmarshaller = jContext.createUnmarshaller(); byte by[] = new String( "<user><age>20</age><date>2014-03-20T16:55:29.687+08:00</date><name>aaaaa</name></user>") .getBytes(); InputStream inputStream = new ByteArrayInputStream(by); User user = (User) unmarshaller.unmarshal(inputStream); System.out.println(user.toString()); } }
xml注解实体转换xml时候:
package org.senssic.jaxrs.bean; import java.util.Date; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlRootElement public class User { // 默认可以不写 @XmlElement private String name; @XmlElement(name = "ageaa"/* 节点名称 */,defaultValue = "20"/* 默认值 */,nillable = true/* 可否为零 */,required = true/* 是否必须 */) private int age; // 为父元素的属性,而不是子节点了 @XmlAttribute private Date date; @XmlElements({ @XmlElement(name = "account",type = String.class),@XmlElement(name = "bean",type = Date.class) }) // list中可以有两种类型,String类和Date类 private List list; // map类型是不可转化类型,需要自定义转换类,比如data类的格式化时间等都需要 @XmlJavaTypeAdapter(MapAdapter.class) private Map<String,Date> map; public Map<String,Date> getMap() { return map; } public void setMap(Map<String,Date> map) { this.map = map; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } 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; } @Override public String toString() { return "名字:" + name + "年龄:" + age + "时间:" + date; } }
map转换类:
package org.senssic.jaxrs.bean; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class MapAdapter extends XmlAdapter<String,Date> { @Override public String marshal(Date arg0) throws Exception { return new Date().toString(); } @Override public Date unmarshal(String arg0) throws Exception { Date date = new Date(); date.setDate(Integer.parseInt(arg0)); return date; } }
例如:时间格式的转换
XmlJavaTypeAdapter(myDate.class)
privatge Date newdate;package org.senssic.jaxrs.bean; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class myDate extends XmlAdapter<String,Date> { @Override public String marshal(Date v) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); return simpleDateFormat.format(v); } @Override public Date unmarshal(String v) throws Exception { return new SimpleDateFormat().parse(v); } }原文链接:https://www.f2er.com/xml/299003.html