Java web ——JAXB 技术—— 提供 javabean 和 xml 映射

 

参考文献:转载的

http://blog.csdn.net/a9529lty/article/details/7211725

 

 

 

=============================================

案例一:代码

 

package package1;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Customer {
 
 String name;
 int age;
 int id;
 
 public String getName() {
  return name;
 }
 
 @XmlElement
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 @XmlElement
 public void setAge(int age) {
  this.age = age;
 }
 
 public int getId() {
  return id;
 }
 
 @XmlAttribute
 public void setId(int id) {
  this.id = id;
 }
 
}

------------------------------------------------------

 

 

package package1;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class JAXBExample {
 public static void main(String[] args) {
 
   Customer customer = new Customer();
   customer.setId(100);
   customer.setName("mkyong");
   customer.setAge(29);
 
   try {
 
  File file = new File("F:\\file.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 
  // output pretty printed
  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
  jaxbMarshaller.marshal(customer, file);
  jaxbMarshaller.marshal(customer, System.out);
 
       } catch (JAXBException e) {
  e.printStackTrace();
       }
 
 }
}

 

--------------------------------------------

 

输出结果是:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>29</age>
    <name>mkyong</name>
</customer>
======================================================

 

对上述案例进行改进:

package package1;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class JAXBExample {
 public static void main(String[] args) {
 
  User user = new User("张三", "zhangsan", "30", "男","广州市天河区", "一名出生的工程师");
 
 
   try {
 
  File file = new File("F:\\file.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 
  // output pretty printed
  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
  jaxbMarshaller.marshal(user, file);
  jaxbMarshaller.marshal(user, System.out);
 
       } catch (JAXBException e) {
             e.printStackTrace();
       }
 
 }
}

----------------------------

package package1;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


 
 
@XmlRootElement
public class User {
 
 
 
 
    private String name;        
 
 
 private String english_name;
 
 
 private String age;        
 
 
 private String sex;       
 
 
 private String address;    
 
 
 private String description; 
   
 public User() {
  //super();
  // TODO Auto-generated constructor stub
 }
 public User(String name, String englishName, String age, String sex,
   String address, String description) {
  
  this.name = name;
  this.english_name = englishName;
  this.age = age;
  this.sex = sex;
  this.address = address;
  this.description = description;
  
 }
 
 
 public String getName() {
  return name;
 }
 @XmlElement
 public void setName(String name) {
  this.name = name;
 }
 public String getEnglish_name() {
  return english_name;
 }
 @XmlElement
 public void setEnglish_name(String englishName) {
  english_name = englishName;
 }
 public String getAge() {
  return age;
 }
 @XmlElement
 public void setAge(String age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 @XmlElement
 public void setSex(String sex) {
  this.sex = sex;
 }
 
  
 public String getDescription() {
  return description;
 }
 @XmlElement
 public void setDescription(String description) {
  this.description = description;
 }
 
 
}  

------------------------------

输出结果:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
    <age>30</age>
    <description>一名出生的工程师</description>
    <english_name>zhangsan</english_name>
    <name>张三</name>
    <sex>男</sex>
</user>

@XmlElement
@XmlAttribute

 

===================================================

 

 把XML转换成Java :    

package package1;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 

import javax.xml.bind.Unmarshaller;

public class JAXBExample {
 public static void main(String[] args) {
 
  
  //创建XML对象,将它保存在F盘下
        File file = new File("F:\\file.xml");
        //声明一个JAXBContext对象
        JAXBContext jaxbContext;
        try {
         //指定映射的类创建JAXBContext对象的上下文
   jaxbContext = JAXBContext.newInstance(User.class);
   //创建转化对象Marshaller
   Unmarshaller u = jaxbContext.createUnmarshaller();
   User user = (User)u.unmarshal(file);
   //输出对象中的内容
   System.out.println("姓名----"+user.getName());
   System.out.println("英文名字----"+user.getEnglish_name());
   System.out.println("年龄----"+user.getAge());
   System.out.println("性别----"+user.getSex());
  // System.out.println("地址----"+user.getAddress());
   System.out.println("描述----"+user.getDescription());

  } catch (JAXBException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 
 }
}      

 

----------------------------------------------------

package package1;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


 
 
@XmlRootElement
public class User {
 
 
 
 
    private String name;        
 
 
 private String english_name;
 
 
 private String age;        
 
 
 private String sex;       
 
 
 private String address;    
 
 
 private String description; 
   
 public User() {
  //super();
  // TODO Auto-generated constructor stub
 }
 public User(String name, String englishName, String age, String sex,
   String address, String description) {
  
  this.name = name;
  this.english_name = englishName;
  this.age = age;
  this.sex = sex;
 // this.address = address;
  this.description = description;
  
 }
 
 
 public String getName() {
  return name;
 }
 @XmlElement
 public void setName(String name) {
  this.name = name;
 }
 public String getEnglish_name() {
  return english_name;
 }
 @XmlElement
 public void setEnglish_name(String englishName) {
  english_name = englishName;
 }
 public String getAge() {
  return age;
 }
 @XmlElement
 public void setAge(String age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 @XmlElement
 public void setSex(String sex) {
  this.sex = sex;
 }
 
  
 public String getDescription() {
  return description;
 }
 @XmlElement
 public void setDescription(String description) {
  this.description = description;
 }
 
 
}

----------------------------

输出:

姓名----张三
英文名字----zhangsan
年龄----30
性别----男
描述----一名出生的工程师
=======================================

改进案例     javabean编程XML   符合对象

 

package package1;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Customer {
 
 String name;
 int age;
 int id;
 
 Address address;
 
 public Address getAddress() {
 return address;
}
@XmlElement
public void setAddress(Address address) {
 this.address = address;
}

public String getName() {
  return name;
 }
 
 @XmlElement
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 @XmlElement
 public void setAge(int age) {
  this.age = age;
 }
 
 public int getId() {
  return id;
 }
 
 @XmlElement
 public void setId(int id) {
  this.id = id;
 }
 
}

---------------------------------------------

package package1;

public class Address {

   String country;
   String city;
  
   public Address(String s1 ,String s2 ){ this.country="中国" ; this.city = "阜阳";}
  
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
  
  
}

---------------------------------------------


package package1;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class JAXBExample {
 public static void main(String[] args) {
 
   Customer customer = new Customer();
   Address  address  = new Address("中国", "阜阳");
 
  
   customer.setId(100);
   customer.setName("马刚");
   customer.setAge(29);     
   customer.setAddress(address);
 
   try {
 
  File file = new File("F:\\file.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 
  // output pretty printed
  jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
  jaxbMarshaller.marshal(customer, file);
  jaxbMarshaller.marshal(customer, System.out);
 
       } catch (JAXBException e) {
                 e.printStackTrace();
       }
 
 }
}
----------------------------------

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <address>
        <city>阜阳</city>
        <country>中国</country>
    </address>
    <age>29</age>
    <id>100</id>
    <name>马刚</name>
</customer>

====================================

 

改进案例     javabean变成XML   多个复合对象变成XML数据:

 

package package1;


import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="users")
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {

 @XmlElement(name="user")
 private List<Customer> list = new ArrayList<Customer>();

 public Users() {
  super();
  // TODO Auto-generated constructor stub
 }

 public Users(List<Customer> list) {
  super();
  this.list = list;
 }

 public void setList(List<Customer> list) {
  this.list = list;
 }

 public List<Customer> getList() {
  return list;
 }
 
 
}

------------------------------------

package package1;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Customer {
 
 String name;
 int age;
 int id;
 
 Address address;
 
 public Address getAddress() {
 return address;
}
@XmlElement
public void setAddress(Address address) {
 this.address = address;
}

public String getName() {
  return name;
 }
 
 @XmlElement
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 @XmlElement
 public void setAge(int age) {
  this.age = age;
 }
 
 public int getId() {
  return id;
 }
 
 @XmlElement
 public void setId(int id) {
  this.id = id;
 }
 
}

--------------------------------

 

package package1;

public class Address {

   String country;
   String city;
  
   public Address(String s1 ,String s2 ){ this.country=s1 ; this.city = s2;}
  
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
  
  
}

----------------------------------------


package package1;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class JAXBExample {
 public static void main(String[] args) {
     
    Users users = new Users();
      
    Customer customer1 = new Customer();
    Address  address1  = new Address("中国", "阜阳");
 
   
    customer1.setId(100);
    customer1.setName("马塬");
    customer1.setAge(29);     
    customer1.setAddress(address1);
   
   
    List<Customer> list = new ArrayList<Customer>();
    list.add(customer1);   
   
   
    Customer customer2 = new Customer();
    Address  address2  = new Address("中国2", "阜阳2");
 
   
    customer2.setId(100);
    customer2.setName("马塬2");
    customer2.setAge(30);     
    customer2.setAddress(address2);
     
    list.add(customer2);   
 
    users.setList(list);
   
    try {
 
   File file = new File("F:\\file.xml");
   JAXBContext jaxbContext = JAXBContext.newInstance(Users.class);
   Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 
   // output pretty printed
   jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
   jaxbMarshaller.marshal(users, file);
   jaxbMarshaller.marshal(users, System.out);
 
        } catch (JAXBException e) {
                  e.printStackTrace();
        }
 
 
 
 
 }
}

--------------------------------------------

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<users>
    <user>
        <address>
            <city>阜阳</city>
            <country>中国</country>
        </address>
        <age>29</age>
        <id>100</id>
        <name>马塬</name>
    </user>
    <user>
        <address>
            <city>阜阳2</city>
            <country>中国2</country>
        </address>
        <age>30</age>
        <id>100</id>
        <name>马塬2</name>
    </user>
</users>
==========================================

 

 把复杂的 XML数据转换成 javabean:

 

package package1;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="users")
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {
 
 @XmlElement(name="user")
 private List<Customer> list = new ArrayList<Customer>();

 public Users() {
  super();
  // TODO Auto-generated constructor stub
 }

 public Users(List<Customer> list) {
  super();
  this.list = list;
 }

 public void setList(List<Customer> list) {
  this.list = list;
 }

 public List<Customer> getList() {
  return list;
 }
  
}

------------------------------------

package package1;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
//@XmlRootElement

public class Customer {
 
 
 String name;
 
 
 int age;
 
 
 int id;
 
 
 Address address;
 
 public Address getAddress() {
 return address;
}
@XmlElement
public void setAddress(Address address) {
 this.address = address;
}

public String getName() {
  return name;
 }
 
 @XmlElement
 public void setName(String name) {
  this.name = name;
 }
 
 public int getAge() {
  return age;
 }
 
 @XmlElement
 public void setAge(int age) {
  this.age = age;
 }
 
 public int getId() {
  return id;
 }
 
 @XmlElement
 public void setId(int id) {
  this.id = id;
 }
 
 
 public String toString(){
 
  String str = "编号" +id+"---" +name+"  来自于"+address.city+"  "+address.country +",年龄"+age;
  return str;
 }
}

------------------------------------

package package1;

import javax.xml.bind.annotation.XmlElement;

public class Address {


   String country;
  


   String city;
  
   public Address(){ super();}
  
   public Address(String s1 ,String s2 ){ this.country=s1 ; this.city = s2;}
  
 public String getCountry() {
  return country;
 }
 
 @XmlElement
 public void setCountry(String country) {
  this.country = country;
 }
 public String getCity() {
  return city;
 }
 
 @XmlElement
 public void setCity(String city) {
  this.city = city;
 }
  
  
}

------------------------------

 

package package1;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.Unmarshaller;

public class JAXBExample {
 public static void main(String[] args) {
 
  //创建XML对象,将它保存在F盘下
    File file = new File("F:\\file.xml");
    //声明一个JAXBContext对象
    JAXBContext jaxbContext;
    try {
     //指定映射的类创建JAXBContext对象的上下文
     jaxbContext = JAXBContext.newInstance(Users.class);
     //创建转化对象Unmarshaller
     Unmarshaller u = jaxbContext.createUnmarshaller();
     //转化指定XML文档为Java对象
     Users users = (Users)u.unmarshal(file);
     List<Customer> list = users.getList();
     for(Customer user:list){
      //输出对象中的内容
      System.out.println("输出每一条记录值:   "+user.toString());
     }
     
    } catch (JAXBException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  
  
  
  
 
 }
}

-------------------------------------

输出每一条记录值:   编号100---马塬  来自于阜阳  中国,年龄29
输出每一条记录值:   编号101---马塬2  来自于阜阳2  中国2,年龄30

 

 

本节知识结束!  希望对您有所启发。以上案例都是测试运行过的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值