@XmlRootElement的配置

NO IMAGE

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)

相關文章

Jboot框架的使用

Linux安裝Zookeeper

安裝React

Java文件流刪除文件夾及文件夾中的文件