• JAXB测试


     1 package com.isoftstone.iics.bizsupport.epartner.fh.xml;
     2 //实体类
     3 import java.util.List;
     4 
     5 import javax.xml.bind.annotation.XmlAccessType;
     6 
     7 import javax.xml.bind.annotation.XmlAccessorType;
     8 import javax.xml.bind.annotation.XmlElement;
     9 import javax.xml.bind.annotation.XmlElementWrapper;
    10 import javax.xml.bind.annotation.XmlRootElement;
    11 import javax.xml.bind.annotation.XmlType;
    12 @XmlAccessorType(XmlAccessType.FIELD)
    13 @XmlRootElement(name="bean")
    14 @XmlType(propOrder={"name","age","phone","email","orderList"})
    15 public class JaxbBean {
    16     
    17     @XmlElement(name="name")
    18     private String name;
    19     
    20     @XmlElement(name="age")
    21     private String age;
    22     
    23     @XmlElement(name="phone")
    24     private String phone;
    25     
    26     @XmlElement(name="email")
    27     private String email;
    28 
    29     @XmlElementWrapper(name="orders")
    30     @XmlElement(name="order")
    31     private List<Order> orderList;
    32 
    33     public String getName() {
    34         return name;
    35     }
    36 
    37     public void setName(String name) {
    38         this.name = name;
    39     }
    40 
    41     public String getAge() {
    42         return age;
    43     }
    44 
    45     public void setAge(String age) {
    46         this.age = age;
    47     }
    48 
    49     public String getPhone() {
    50         return phone;
    51     }
    52 
    53     public void setPhone(String phone) {
    54         this.phone = phone;
    55     }
    56 
    57     public String getEmail() {
    58         return email;
    59     }
    60 
    61     public void setEmail(String email) {
    62         this.email = email;
    63     }
    64 
    65     public List<Order> getOrderList() {
    66         return orderList;
    67     }
    68 
    69     public void setOrderList(List<Order> orderList) {
    70         this.orderList = orderList;
    71     }
    72 
    73     public JaxbBean(String name, String age, String phone, String email, List<Order> orderList) {
    74         super();
    75         this.name = name;
    76         this.age = age;
    77         this.phone = phone;
    78         this.email = email;
    79         this.orderList = orderList;
    80     }
    81 
    82     public JaxbBean() {
    83         super();
    84         // TODO Auto-generated constructor stub
    85     }
    86 
    87     
    88     
    89 
    90 }

    测试类

     1 package com.isoftstone.iics.bizsupport.epartner.fh.xml;
     2 
     3 import java.io.StringReader;
     4 import java.io.StringWriter;
     5 import java.util.ArrayList;
     6 import java.util.List;
     7 
     8 import javax.xml.bind.JAXBContext;
     9 import javax.xml.bind.JAXBException;
    10 import javax.xml.bind.Marshaller;
    11 
    12 import javax.xml.bind.Unmarshaller;
    13 
    14 import org.junit.Test;
    15 
    16 public class JaxbTest {
    17     
    18     
    19     @Test
    20     public void test() throws JAXBException{
    21         JaxbBean bean=new JaxbBean();
    22         bean.setAge("24");
    23         bean.setEmail("512178509@qq.com");
    24         bean.setName("冯浩");
    25         bean.setPhone("18295789020");
    26         List<Order> orderList=new ArrayList<Order>();
    27         
    28         Order order=new Order();
    29         order.setId("1");
    30         order.setProduct("《java特种兵》");
    31         order.setNumber("1");
    32         orderList.add(order);
    33         
    34         Order order1=new Order();
    35         order1.setId("2");
    36         order1.setProduct("《spring实战》");
    37         order1.setNumber("1");
    38         orderList.add(order1);
    39 //        
    40         bean.setOrderList(orderList);
    41         String xml = this.convertToXml(bean);
    42         System.out.println(xml);
    43     }
    44 
    45     public String convertToXml(Object obj) throws JAXBException{
    46         JAXBContext context=JAXBContext.newInstance(obj.getClass());
    47         Marshaller marshaller = context.createMarshaller();
    48         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    49         marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
    50         StringWriter writer=new StringWriter();
    51         marshaller.marshal(obj, writer);
    52         return writer.toString();
    53     }
    54     
    55     public <T> T convertToJavaBean(String xml,Class<T> c) throws JAXBException{
    56         JAXBContext context=JAXBContext.newInstance(c);
    57         Unmarshaller unmarshaller = context.createUnmarshaller();
    58         T unmarshal =(T) unmarshaller.unmarshal(new StringReader(xml));
    59         return unmarshal;
    60     }
    61 }

    只是简单的测试

  • 相关阅读:
    eclipse快捷键
    Struts2框架(8)---Struts2的输入校验
    Struts2框架(5)---result结果集
    Spring框架(6)---AspectJ实现AOP
    Spring框架(4)---AOP讲解铺垫
    Spring框架(3)---IOC装配Bean(注解方式)
    Spring框架(2)---IOC装配Bean(xml配置方式)
    Spring框架(1)---Spring入门
    Mybatis框架(5)---动态sql
    Mybatis框架(4)---输入输出映射
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/6817190.html
Copyright © 2020-2023  润新知