Java教程

JavaBean与xml互转的方法详解,java编译原理pdf脚本之家

本文主要是介绍JavaBean与xml互转的方法详解,java编译原理pdf脚本之家,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

@XmlElement(required = true)

private String author;

@XmlElement(name = “price_1”, required = true)

private float price;

@XmlElement

private Date calendar;

@XmlAttribute

private Integer id;

/**

  • @return

*/

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public float getPrice() {

return price;

}

public void setPrice(float price) {

this.price = price;

}

public Date getCalendar() {

return calendar;

}

public void setCalendar(Date calendar) {

this.calendar = calendar;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

@Override

public String toString() {

return “Book [author=” + author + “, price=” + price + “, calendar=” + calendar + “, id=” + id + “]”;

}

}

(2)test

package com.guor.demo.beanToXml;

import java.util.Date;

import org.junit.Test;

import javax.xml.bind.JAXBException;

@SuppressWarnings(“unused”)

public class JaxbTest1 {

/**

  • @throws JAXBException

*/

@Test

public void showMarshaller() {

Book book = new Book();

book.setId(100);

book.setAuthor(“lin”);

book.setCalendar(new Date());

book.setPrice(23.56f);

String str = JaxbUtil.converTomXml(book);

System.out.println(str);

}

/**

  • @throws JAXBException

*/

@Test

public void showUnMarshaller() {

String str = “<?xml version =\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>” + “<book id =“100”>”

  • “lin” + “2018-9-3T11:58.006” + “<price_1>23.56</price_1>”

  • “”;

Book book = JaxbUtil.converyToJavaBean(str, Book.class);

System.out.println(book);

}

}

(3)控制台输出

3、类中包含复杂对象的转换

(1)实体类1

package com.guor.demo.beanToXml;

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;

import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name = “student”)

@XmlType(propOrder = {})

public class Student {

@XmlAttribute

private Integer id;

@XmlElement

private String name;

@XmlElement(name = “role”)

private Role role;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Role getRole() {

return role;

}

public void setRole(Role role) {

this.role = role;

}

@Override

public String toString() {

return “Student [id=” + id + “, name=” + name + “, role=” + role + “]”;

}

}

(2)实体类2

package com.guor.demo.beanToXml;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(propOrder = { “name”, “desc” })

public class Role {

@XmlElement

private String name;

@XmlElement

private String desc;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDesc() {

return desc;

}

public void setDesc(String desc) {

this.desc = desc;

}

@Override

public String toString() {

return “Role [name=” + name + “,desc=” + desc + “]”;

}

}

(3)test

package com.guor.demo.beanToXml;

import org.junit.Test;

public class JaxbTest2 {

@Test

public void showMarshaller() {

Student student = new Student();

student.setId(12);

student.setName(“nihao”);

Role role = new Role();

role.setDesc(“管理”);

role.setName(“班长”);

student.setRole(role);

String str = JaxbUtil.converTomXml(student);

System.out.println(str);

}

@Test

public void showUnMarshaller() {

String str = “<?xml version =\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>” + “<student id =“12”>”

  • “nihao” + “” + “ 管理 ” + “班长” + “” + “”;

Student student = JaxbUtil.converyToJavaBean(str, Student.class);

System.out.println(student);

}

}

(4)控制台输出

4、集合对象的转换(同样适用于Set)

(1)实体类1

package com.guor.demo.beanToXml;

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.XmlElementWrapper;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name = “country”)

@XmlType(propOrder = { “name”, “provinceList” })

public class Country {

@XmlElement(name = “country_name”)

private String name;

@XmlElementWrapper(name = “provinces”)

@XmlElement(name = “province”)

private List provinceList;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public List getProvinceList() {

return provinceList;

}

public void setProvinceList(List provinceList) {

this.provinceList = provinceList;

}

@Override

public String toString() {

return "Country [name = " + name + “,provinceList” + provinceList + “]”;

}

}

(2)实体类2

package com.guor.demo.beanToXml;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(propOrder = { “name”, “provCity” })

public class Province {

@XmlElement(name = “province_name”)

private String n

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ame;

@XmlElement(name = “prov_city”)

private String provCity;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getProvCity() {

return provCity;

}

public void setProvCity(String provCity) {

this.provCity = provCity;

}

@Override

public String toString() {

return “Province [name=” + name + “, provCity=” + provCity + “]”;

}

}

(3)test

package com.guor.demo.beanToXml;

import java.util.List;

import java.util.ArrayList;

import org.junit.Test;

public class JaxbTest3 {

@Test

public void showShaller() {

Country country = new Country();

country.setName(“中国”);

List list = new ArrayList();

Province province = new Province();

province.setName(“辽宁省”);

province.setProvCity(“大连市”);

Province province2 = new Province();

province2.setName(“黑龙江省”);

province2.setProvCity(“哈尔滨市”);

list.add(province);

list.add(province2);

country.setProvinceList(list);

String str = JaxbUtil.converTomXml(country);

这篇关于JavaBean与xml互转的方法详解,java编译原理pdf脚本之家的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!