webservice之使用jaxb把xml转换Object或把对象转换成xml文件

 

//使用jaxb把xml转换Object或把对象转换成xml文件
public class Xml2Object {

 public static void main(String[] args) {
  
  //转换成对象
//  String xmlFile = "G:\\workspace\\jaxb\\src\\user.xml";
//  Class<User> clazz = User.class;
//  User u1 = xml2Obj(xmlFile, clazz);
//  System.out.println(u1);
  
  //使用jaxb转换成xml文件
  Class<User> clazz = User.class;
  User user = new User();
  user.setName("lisi");
  user.setAge(20);
  user.setBirthday(new Date());
  String xmlFile = "G:\\workspace\\jaxb\\src\\user3.xml";
  FileOutputStream os = null;
  try {
   os = new FileOutputStream(xmlFile);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  obj2Xml(clazz, user, os);
 }

 public static <T> T xml2Obj(String xmlFile, Class<T> clazz){
  try{
         JAXBContext jc = JAXBContext.newInstance(clazz); 
         Unmarshaller u = jc.createUnmarshaller(); 
         InputStream is = new FileInputStream(xmlFile); 
         return (T)u.unmarshal(is);
  }catch(Exception e){
   e.printStackTrace();
  }
  return null;
 }
 
 public static <T> void obj2Xml(Class<T> clazz, T o, OutputStream os){
  try{
   JAXBContext context = JAXBContext.newInstance(clazz); 
         Marshaller marshaller = context.createMarshaller();  
         //编码格式  
         marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8"); 
         //是否格式化生成的xml串     
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
         //是否省略xml头信息(<?xml version="1.0" encoding="gb2312" standalone="yes"?>) 
         marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); 
        
         marshaller.marshal(o, os);
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

 

/**
 * @XmlRootElement:根节点
       @XmlAttribute:该属性作为xml的attribute
       @XmlElement:该属性作为xml的element

 *@XmlElementWrapper(name="items")
 *@XmlType(name ="shop", propOrder = {"name", "number","describer", "address","orders"})
 *
 *@XmlJavaTypeAdapter(XmlyyyyMMddHHmmDateAdapter.class)
 *
 */

@XmlRootElement(name = "user")//设置根节点
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
 
 @XmlElement(name="name",required=true,nillable=true)
 private String name;
 private int age;
 
 @XmlJavaTypeAdapter(DateConverter.class)
 private Date birthday;
 
 
 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 Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 
 public String toString() {
  StringBuffer s = new StringBuffer();
  s.append("name=").append(name).append(",");
  s.append("age=").append(age).append(",");
  s.append("birthday=").append(birthday);
  return s.toString();
 }
 
}

user.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<user>
 <name>xx</name>
 <age>12</age>
 <birthday>2017-01-02</birthday>
</user>

 

import javax.xml.bind.annotation.adapters.XmlAdapter;

 

//日期转换器:把Date类型转换成字符串格式
public class DateConverter extends XmlAdapter<String, Date>{

     private SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd"); 
  
     public Date unmarshal(String v) throws Exception { 
         if (v == null||"".equals(v)) { 
             return null; 
         } 
         return yyyyMMdd.parse(v); 
     } 
  
     public String marshal(Date v) throws Exception { 
         return yyyyMMdd.format(v); 
     } 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值