标签:des element 配置 填充 desktop pen pass 数据 imp
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
User类
@XmlRootElement
public class User{
private Integer id;
private String name;
// gettters and setters
}
xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
<id>1</id>
<name>叶</name>
</user>
获取配置
User user = JAXB.unmarshal(new File("C:\\Users\\26401\\Desktop\\demo.xml"), User.class);
User user = new User();
user.setName("小叶");
user.setId(2);
JAXB.marshal(user, new File("C:\\Users\\26401\\Desktop\\demo.xml"));
结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
<id>2</id>
<name>小叶</name>
</user>
@XmlAccessorType(XmlAccessType.NONE)
public class User{
@XmlElement
private Integer id;
@XmlElement(name="username")
private String name;
@XmlAttribute
private String nameDescribe;
@XmlTransient
private Integer age;
@XmlElement
public Integer getPassAge() {
return age;
}
public void setPassAge(Integer val) {}
public User(){}
public User(Integer id, String name, String nameDescribe, Integer age) {
this.id = id;
this.name = name;
this.nameDescribe = nameDescribe;
this.age = age;
}
}
写数据
User user = new User(1, "叶家伟", "用户姓名", 18);
JAXB.marshal(user, new File("C:\\Users\\26401\\Desktop\\demo.xml"));
结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user nameDescribe="用户姓名">
<id>1</id>
<username>叶家伟</username>
<passAge>18</passAge>
</user>
本文章是java成神的系列文章之一
如果你想知道,但是本文没有的,请下方留言
我会第一时间总结出来并发布填充到本文
标签:des element 配置 填充 desktop pen pass 数据 imp
原文地址:https://www.cnblogs.com/ye-hcj/p/9750356.html