简单例子step2
Book.java
1
package step2;
2![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
3
import javax.xml.bind.annotation.XmlAccessType;
4
import javax.xml.bind.annotation.XmlAccessorType;
5
/* @XmlAccessorType此注释可控制类中的属性和字段的默认序列化
6
* value=XmlAccessType.PROPERTY表示JAXB 绑定类中的每个
7
*获取方法/设置方法对将会自动绑定到 XML
8
* */
9
@XmlAccessorType(value = XmlAccessType.PROPERTY)
10
public class Book {
11
12
private String id;
13
private String name;
14
private float price;
15
16
public String getId() {
17
return id;
18
}
19
public void setId(String id) {
20
this.id = id;
21
}
22
public String getName() {
23
return name;
24
}
25
public void setName(String name) {
26
this.name = name;
27
}
28
public float getPrice() {
29
return price;
30
}
31
public void setPrice(float price) {
32
this.price = price;
33
}
34
@Override
35
public String toString() {
36
return "Book [id=" + id + ",name=" + name + ",price=" + price + "]";
37
}
38
}
39![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
2
![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
3
![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
4
![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
7
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
8
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
12
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
14
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
15
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
18
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
21
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
24
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
27
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
30
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
33
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
35
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](http://www.blogjava.net/images/OutliningIndicators/InBlock.gif)
37
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif)
39
![](http://www.blogjava.net/images/OutliningIndicators/None.gif)
Customer.java
1
package step2;
2
import java.util.Set;
3![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
4
import javax.xml.bind.annotation.XmlElement;
5
import javax.xml.bind.annotation.XmlElementWrapper;
6
import javax.xml.bind.annotation.XmlRootElement;
7
import javax.xml.bind.annotation.XmlType;
8![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
@XmlRootElement
10
@XmlType(propOrder ={ "id", "name", "age","book"})
11
publicclass Customer {
12
String name;
13
int age;
14
int id;
15
Set<Book> book;
16
@XmlElement(name="name")
17
public String getName() {
18
return name;
19
}
20![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
21
publicvoid setName(String name) {
22
this.name = name;
23
}
24![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
25
@XmlElement(name="age")
26
publicint getAge() {
27
return age;
28
}
29![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
30
publicvoid setAge(int age) {
31
this.age = age;
32
}
33
@XmlElement(name="id")
34
publicint getId() {
35
return id;
36
}
37![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
38
publicvoid setId(int id) {
39
this.id = id;
40
}
41![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
42
43
@Override
44
public String toString() {
45
return"Customer [id="+ id +",name="+ name +",age="+ age +",book="+ book +"]";
46
}
47
@XmlElementWrapper(name="books")
48
@XmlElement(name="book")
49
public Set<Book> getBook() {
50
return book;
51
}
52![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
53
publicvoid setBook(Set<Book> book) {
54
this.book = book;
55
}
56![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
57
58
}
59![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
30
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
32
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
34
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
36
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
38
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
40
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
42
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
43
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
44
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
46
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
48
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
49
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
50
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
51
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
53
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
54
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
55
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
57
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
58
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
59
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
Object2XmlDemo.java
1
package step2;
2![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
3
import java.io.File;
4
import java.util.HashSet;
5
import java.util.Set;
6![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
import javax.xml.bind.JAXBContext;
8
import javax.xml.bind.JAXBException;
9
import javax.xml.bind.Marshaller;
10![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
11
//Marshaller
12
publicclass Object2XmlDemo {
13
publicstaticvoid main(String[] args) {
14![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
15
Customer customer =new Customer();
16
customer.setId(100);
17
customer.setName("suo");
18
customer.setAge(29);
19
20
Book book =new Book();
21
book.setId("1");
22
book.setName("哈里波特");
23
book.setPrice(100);
24
25
Book book2 =new Book();
26
book2.setId("2");
27
book2.setName("苹果");
28
book2.setPrice(50);
29
30
Set<Book> bookSet =new HashSet<Book>();
31
bookSet.add(book);
32
bookSet.add(book2);
33
34
customer.setBook(bookSet);
35
36
try{
37
File file =new File("H:\\Xfile24.xml");
38
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
39
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
40
// output pretty printed
41
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
42
jaxbMarshaller.marshal(customer, file);
43
jaxbMarshaller.marshal(customer, System.out);
44
}catch (JAXBException e) {
45
e.printStackTrace();
46
}
47
}
48
}
49![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
20
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
26
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
30
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
31
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
32
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
33
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
34
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
35
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
36
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
38
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
39
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
40
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
41
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
42
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
43
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
44
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
46
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
49
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
执行效果
1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
<customer>
3
<id>100</id>
4
<name>suo</name>
5
<age>29</age>
6
<books>
7
<book>
8
<id>1</id>
9
<name>鍝堥噷娉㈢壒</name>
10
<price>100.0</price>
11
</book>
12
<book>
13
<id>2</id>
14
<name>鑻规灉</name>
15
<price>50.0</price>
16
</book>
17
</books>
18
</customer>
19![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
2
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
3
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
4
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
5
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
6
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
7
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
8
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
9
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
10
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
11
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
12
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
13
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
14
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
15
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
16
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
17
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
18
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
19
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)