Webservice uses jaxb to convert xml to Object or convert object to xml file

 

//Use jaxb to convert xml to Object or convert object to xml file
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"); 
         //Whether to format the generated xml string     
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
         //Whether to omit the xml header information (<?xml version="1.0" encoding="gb2312" standalone="yes"?>) 
         marshaller .setProperty(Marshaller.JAXB_FRAGMENT, false); 
        
         marshaller.marshal(o, os);
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

 

/**
 * @XmlRootElement: The root node
       @XmlAttribute: This attribute is used as an attribute of xml
       @XmlElement: This attribute is used as an element of xml

 *@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 is as follows:

<?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 Converter : Convert Date type to string format
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); 
     } 
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325974105&siteId=291194637