实体类:
1 package xml; 2 3 import javax.xml.bind.annotation.XmlRootElement; 4 5 @XmlRootElement 6 public class User { 7 String name; 8 String english_name; 9 String age; 10 String sex; 11 String address; 12 String description; 13 public User(){} 14 15 public User(String name, String english_name, String age, String sex, 16 String address, String description) { 17 this.name=name; 18 this.english_name=english_name; 19 this.age=age; 20 this.sex=sex; 21 this.address=address; 22 this.description=description; 23 } 24 public String getName() { 25 return name; 26 } 27 public void setName(String name) { 28 this.name = name; 29 } 30 public String getEnglish_name() { 31 return english_name; 32 } 33 public void setEnglish_name(String english_name) { 34 this.english_name = english_name; 35 } 36 public String getAge() { 37 return age; 38 } 39 public void setAge(String age) { 40 this.age = age; 41 } 42 public String getSex() { 43 return sex; 44 } 45 public void setSex(String sex) { 46 this.sex = sex; 47 } 48 public String getAddress() { 49 return address; 50 } 51 public void setAddress(String address) { 52 this.address = address; 53 } 54 public String getDescription() { 55 return description; 56 } 57 public void setDescription(String description) { 58 this.description = description; 59 } 60 61 62 63 }
一.java对象转化成xml文件
1 package xml; 2 3 import java.io.File; 4 5 import javax.xml.bind.JAXBContext; 6 import javax.xml.bind.JAXBException; 7 import javax.xml.bind.Marshaller; 8 9 /** 10 * 将类映射成xml 11 * @author Administrator 12 * 13 */ 14 public class Demo1 { 15 public static void main(String[] args) { 16 //创建xml对象,将他保存在指定位置 17 File file = new File("D:\\javaItem\\xml\\src\\xml\\1.xml"); 18 //申明一个JAXBContext对象 19 JAXBContext jaxbContext; 20 try { 21 //指定映射的类,创建JAXBContext对象的上下文 22 jaxbContext = JAXBContext.newInstance(User.class); 23 //创建转化对象Marshaller 24 Marshaller m = jaxbContext.createMarshaller(); 25 //创建xml文件中的数据 26 User user = new User("张三", "zhangsnan", "30", "男", "安徽合肥", "软件工程师"); 27 //将Java类中的User对象转化到XML 28 m.marshal(user, file); 29 System.out.println("转化完成"); 30 } catch (JAXBException e) { 31 e.printStackTrace(); 32 } 33 } 34 }
二.xml文件转化为java对象
1 package xml; 2 3 import java.io.File; 4 5 import javax.xml.bind.JAXBContext; 6 import javax.xml.bind.JAXBException; 7 import javax.xml.bind.Unmarshaller; 8 9 10 11 public class Demo2 { 12 public static void main(String[] args) { 13 File file=new File("D:\\javaItem\\xml\\src\\xml\\1.xml"); 14 JAXBContext jaxbContext; 15 try { 16 jaxbContext=JAXBContext.newInstance(User.class); 17 Unmarshaller u=jaxbContext.createUnmarshaller(); 18 User user=(User)u.unmarshal(file); 19 System.out.println("姓名:"+user.getName()); 20 System.out.println("英文名字:"+user.getEnglish_name()); 21 System.out.println("年龄:"+user.getAge()); 22 System.out.println("性别:"+user.getSex()); 23 System.out.println("地址:"+user.getAddress()); 24 System.out.println("描述:"+user.getDescription()); 25 } catch (JAXBException e) { 26 e.printStackTrace(); 27 } 28 } 29 }
时间: 2024-10-03 23:09:33