java xml和对象bean 互转

修改别人的。

 

package cn.wjj;

public class ClassRoom {
 private int id;
 private String name;
 private int grade;
 private String messageId;
 

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getGrade() {
  return grade;
 }

 public void setGrade(int grade) {
  this.grade = grade;
 }

 public ClassRoom(int id, String name, int grade,String messageId) {
  super();
  this.id = id;
  this.name = name;
  this.grade = grade;
  this.messageId=messageId;
 }

 //无参够着函数一定需要,否则JXBContext无法正常解析。 
 public ClassRoom() {
  super();
 }

 public String getMessageId() {
  return messageId;
 }

 public void setMessageId(String messageId) {
  this.messageId = messageId;
 }

}

 

package cn.wjj;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student {
 private int id;
 private String name;
 private int age;
 private String school;
 private ClassRoom classRoom;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 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 ClassRoom getClassroom() {
  return classRoom;
 }

 public void setClassroom(ClassRoom classroom) {
  this.classRoom = classroom;
 }

 public Student(int id, String name, int age, ClassRoom classroom,String school) {
  super();
  this.id = id;
  this.name = name;
  this.age = age;
  this.classRoom = classroom;
  this.school=school;
 }

 //无参够着函数一定需要,否则JXBContext无法正常解析。 
 public Student() {
  super();
 }

 public String getSchool() {
  return school;
 }

 public void setSchool(String school) {
  this.school = school;
 }
}

package cn.wjj;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.*;

 

public class XmlUtil {

 public static String toXML(Object obj) {
  try {
   JAXBContext context = JAXBContext.newInstance(obj.getClass());

   Marshaller marshaller = context.createMarshaller();
   marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
   marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
   marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xm头声明信息

   StringWriter writer = new StringWriter();
   marshaller.marshal(obj, writer);
   return writer.toString();
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }

 @SuppressWarnings("unchecked")
 public static <T> T fromXML(String xml, Class<T> valueType) {
  try {
   JAXBContext context = JAXBContext.newInstance(valueType);
   Unmarshaller unmarshaller = context.createUnmarshaller();
   return (T) unmarshaller.unmarshal(new StringReader(xml));
  } catch (Exception e) {
   throw new RuntimeException(e.getMessage());
  }
 }
}

 

package cn.wjj;

 


public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
 
  
  ClassRoom classroom = new ClassRoom(1, "软件工程", 4,"010");
  Student student = new Student(101, "张三", 22, classroom, "1");
  
  String s=XmlUtil.toXML(student);
  System.out.println(s);
  
//  String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><student><age>25</age><classroom><grade>5</grade><id>1</id>"
//    + "<name>软件工程2</name></classroom><id>102</id><name>李四</name></student>";
  
  Student stu2=XmlUtil.fromXML(s, Student.class);
  System.out.println(stu2.getAge());
  
  
  
  

 }

}

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
    <age>22</age>
    <classroom>
        <grade>4</grade>
        <id>1</id>
        <messageId>010</messageId>
        <name>软件工程</name>
    </classroom>
    <id>101</id>
    <name>张三</name>
    <school>1</school>
</student>

22

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值