- JAXB概念:
java对象←→XML
- 相关类和接口:
JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。
Marshaller接口,将Java对象序列化为XML数据。
Unmarshaller接口,将XML数据反序列化为Java对象。
- 相关注解:
@XmlRootElement,将Java类或枚举类型映射到XML元素。(name属性:xml根标签的名字)
@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。
@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。
@XmlType,将Java类或枚举类型映射到XML模式类型(propOrder={“”“”}:xml元素的顺序)
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。 - 代码案例
package com.javab; import javax.xml.bind.annotation.*; import java.io.Serializable; import java.util.Map; @XmlType(name="yhqtest", propOrder = {"userName","map","role","menu","bibi"}) @XmlRootElement(name = "rootelement") @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL) public class User implements Serializable { private String userName; private int age; private String role; private String bibi; private Menu menu; private Map<String,Menu> map; public User() { } public User(String userName, int age, String role, String bibi) { this.userName = userName; this.role = role; this.age = age; this.bibi = bibi; } public Map<String, Menu> getMap() { return map; } public void setMap(Map<String, Menu> map) { this.map = map; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @XmlAttribute public int getAge() { return age; } public void setAge(int age) { this.age = age; } @XmlElement(nillable=true) public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getBibi() { return bibi; } public void setBibi(String bibi) { this.bibi = bibi; } @XmlElement public Menu getMenu() { return menu; } public void setMenu(Menu menu) { this.menu = menu; } @Override public String toString() { return "User{" + "userName='" + userName + '\'' + ", age=" + age + ", role='" + role + '\'' + ", menu=" + menu + '}'; } }
package com.javab; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Menu { private String name; private String id; public Menu() { } public Menu(String name, String id) { this.name = name; this.id = id; } @Override public String toString() { return "Menu{" + "name='" + name + '\'' + ", id='" + id + '\'' + '}'; } }
package com.javab; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import java.io.File; public class JaxbUtil { public static void convertToXml(Object obj, File file) { try { JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Marshaller marshaller = jaxbContext.createMarshaller(); //格式化输出,即按标签自动换行,否则就是一行输出 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //设置编码(默认编码就是utf-8) marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //是否省略xml头信息,默认不省略(false) marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); marshaller.marshal(obj, file); //控制台输出 marshaller.marshal(obj,System.out); } catch (JAXBException e) { e.printStackTrace(); } } public static <T> T convertToJavaBean(Class<T> clz, File file) { try { JAXBContext jaxbContext = JAXBContext.newInstance(clz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); T t = (T) unmarshaller.unmarshal(file); return t; } catch (JAXBException e) { e.printStackTrace(); } return null; } }
package com.javab; import java.io.File; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { (new Test()).test2(); } public void test1(){ File file = new File("C:\\Users\\yaohuiqin\\Desktop\\yhq.xml"); User user = JaxbUtil.convertToJavaBean(User.class, file); System.out.println(user); } public void test2(){ User user = new User(); user.setAge(11); user.setRole("学生"); user.setBibi("bibi"); Menu menu = new Menu("回锅肉","1"); user.setMenu(menu); user.setUserName("yhq"); Menu menuMap = new Menu("menuMap","2"); Map<String,Menu> map = new HashMap<>(); map.put("menuMapKey",menuMap); user.setMap(map); File file = new File("C:\\Users\\yaohuiqin\\Desktop\\yhq.xml"); JaxbUtil.convertToXml(user,file); } }
Σ(っ°Д°;)っ