XML报文转JAVA对象-JAVA对象转XML报文

前端之家收集整理的这篇文章主要介绍了XML报文转JAVA对象-JAVA对象转XML报文前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JAVA 实体类Person.java

  1. public class Person {
  2. private int id;
  3. private String username;
  4. private String age;
  5. private String address;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getAge() {
  19. return age;
  20. }
  21. public void setAge(String age) {
  22. this.age = age;
  23. }
  24. public String getAddress() {
  25. return address;
  26. }
  27. public void setAddress(String address) {
  28. this.address = address;
  29. }
  30. //无参构造函数
  31. public Person(){
  32. super();
  33. }
  34. //有参构造函数
  35. public Person(int id,String username,String age,String address){
  36. this.id=id;
  37. this.username=username;
  38. this.age=age;
  39. this.address=address;
  40. }
  41.  
  42. }

第二个JAVA实体类,把上面声明的Person 实体作为一个变量存在

注意 @XmlRootElement 的注解的使用

  1. import javax.xml.bind.annotation.XmlRootElement;
  2.  
  3. @XmlRootElement
  4. public class ObjectDemo {
  5. private String name;
  6. private String sex;
  7. private Person person;
  8. /**
  9. * @return the person
  10. */
  11. public Person getPerson() {
  12. return person;
  13. }
  14. /**
  15. * @param person the person to set
  16. */
  17. public void setPerson(Person person) {
  18. this.person = person;
  19. }
  20. /**
  21. * @return the name
  22. */
  23. public String getName() {
  24. return name;
  25. }
  26. /**
  27. * @param name the name to set
  28. */
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. /**
  33. * @return the sex
  34. */
  35. public String getSex() {
  36. return sex;
  37. }
  38. /**
  39. * @param sex the sex to set
  40. */
  41. public void setSex(String sex) {
  42. this.sex = sex;
  43. }
  44. //有参构造方法
  45. public ObjectDemo(String name,String sex,Person person){
  46. this.name=name;
  47. this.sex=sex;
  48. this.person=person;
  49. }
  50. //无参构造方法
  51. public ObjectDemo(){
  52. super();
  53. }
  54. }


测试方法

  1. import java.io.StringReader;
  2.  
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. import javax.xml.bind.Unmarshaller;
  7.  
  8. import org.junit.Test;
  9.  
  10. import com.demo.vo.ObjectDemo;
  11. import com.demo.vo.Person;
  12.  
  13. public class XmlToObject {
  14.  
  15. @Test
  16. public void XMLTobean() {
  17. Person p = new Person(1,"小红","10","北京");
  18. ObjectDemo ob = new ObjectDemo("小明","11",p);
  19.  
  20. try {
  21. JAXBContext context = JAXBContext.newInstance(ObjectDemo.class);
  22. Marshaller marshaller = context.createMarshaller();
  23. marshaller.marshal(ob,System.out);
  24. } catch (JAXBException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. }
  28.  
  29. }
  30. @Test
  31. public void beanToXML() {
  32. String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
  33. "<objectDemo>" +
  34. "<name>小明</name>" +
  35. "<person>" +
  36. "<address>北京</address>" +
  37. "<age>10</age>" +
  38. "<id>1</id>" +
  39. "<username>小红</username>" +
  40. "</person>" +
  41. "<sex>11</sex>" +
  42. "</objectDemo>";
  43. try {
  44. JAXBContext context = JAXBContext.newInstance(ObjectDemo.class);
  45. Unmarshaller unmarshaller = context.createUnmarshaller();
  46. ObjectDemo obj = (ObjectDemo)unmarshaller.unmarshal(new StringReader(xml));
  47. System.out.println(obj.getName());
  48. System.out.println(obj.getPerson().getAddress());
  49. } catch (JAXBException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }
  54.  
  55. }

猜你在找的XML相关文章