Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
131 lines (111 loc) · 3.56 KB

File metadata and controls

131 lines (111 loc) · 3.56 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package xmlTest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.File;
import java.io.Serializable;
import java.util.Date;
//JavaBean代码
@XmlType(propOrder = {"role","userName","birthDay"})
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
class User implements Serializable {
private String userName;
private int age;
private String role;
private String bibi;
private Date birthDay;
public User() {
}
public User(String userName, int age, String role, String bibi,Date date) {
this.userName = userName;
this.age = age;
this.role = role;
this.bibi = bibi;
this.birthDay=date;
}
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
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
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@XmlTransient
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 + '\'' +
'}';
}
}
//测试
public class JABXTest {
public static void saveXmlTest() {
User user = new User("陈本布衣", 2018, "超级管理员","瞎哔哔",new Date());
File file = new File("D:\\java\\xml\\user.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();
}
}
public static void main(String[] args) {
// saveXmlTest();
// getUserTest();
User user = new User("陈本布衣", 2018, "超级管理员","瞎哔哔",new Date());
JABXUtils.convertToXmlB(user,new File("D:\\java\\xml\\user.xml"));
}
public static void getUserTest() {
File file = new File("D:\\java\\xml\\user.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();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.