-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
38 lines (28 loc) · 1010 Bytes
/
Copy pathTest.java
File metadata and controls
38 lines (28 loc) · 1010 Bytes
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
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Test {
public static void main(String[] args) throws JAXBException {
User u = new User();
u.setId(1);
u.setName("zhanghao");
Address address = new Address();
address.setProvince("shannxi");
address.setCity("baoji");
address.setStreet("zhongshanRoad");
u.setAddress(address);
UserList user = new UserList();
user.add(u);
user.add(u);
user.add(u);
JAXBContext ctx = JAXBContext.newInstance(UserList.class);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
File file= new File("c:\\fff.xml");
marshaller.marshal(user, System.out);
marshaller.marshal(user, file);
}
}