@XmlRootElement的配置

jdk1.8引入了将字段转换为xml文件的东西,先来个编程看看,

import java.io.Serializable;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "model")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class User implements Serializable {

private String userName;
private int age;
private String role;
private String bibi;

public User() {
}

public User(String userName, int age, String role, String bibi) {
    this.userName = userName;
    this.age = age;
    this.role = role;
    this.bibi = bibi;
}


public String getUserName() {
	return userName;
}

public void setUserName(String userName) {
	this.userName = userName;
}

public int getAge() {
	return age;
}

public void setAge(int age) {
	this.age = age;
}

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;
}

@Override
public String toString() {
    return "User{" +
            "userName='" + userName + '\'' +
            ", age=" + age +
            ", role='" + role + '\'' +
            ", bibi='" + bibi + '\'' +
            '}';
}
}
复制代码

再写个测试类:

import java.io.File;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

import org.junit.Test;

public class test {

 @Test

 public void saveXmlTest() {

  User user = new User("ggg", 18, "aaa","qq");
  File file = new File("D://hello.xml");
  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(user, file);
  } catch (JAXBException e) {
      e.printStackTrace();
  }
}

@Test
public void getUserTest() {

  File file = new File("D://hello.xml");
  try {
      JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      User user = (User) unmarshaller.unmarshal(file);
      System.out.println(user.toString());
  } catch (JAXBException e) {
      e.printStackTrace();
  }
}
}
复制代码

saveXmlTest()方法是将实体类字段写进xml文件里面,getUserTest()方法是将hello.xml中的字段读取出来;

常用的注解有: @XmlRootElement,将Java类或枚举映射成XML元素根节点,是唯一一个必须注解,name属性指定根节点名称,不指定默认为类名的小写;

@XmlAccessorType,控制字段或属性的序列化。属性XmlAccessType有4个常量值:FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标注)字段到XML;PROPERTY表示java对象中所有通过getter/setter方式绑定成属性到XML;PUBLIC_MEMBER表示Java对象中所有的public访问权限的成员变量和通过getter/setter方式访问的成员变量,该值为默认值;NONE表示Java对象的所有属性都不映射为XML的元素;

@XmlTransient ,用于标示在由Java对象映射XML时,忽略此属性,在生成的XML文件中将不出现此元素。(该文章引用了别人的文章,详情看https://www.cnblogs.com/chenbenbuyi/p/8283657.html)

你可能感兴趣的:(java)