JAXB
来源:互联网 发布:学校硬件软件 编辑:程序博客网 时间:2024/05/08 01:46
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
JAXB 2.0是JDK 1.6的组成部分。JAXB 2.2.3是JDK 1.7的组成部分。
JDK中JAXB相关的重要Class和Interface:
JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。 Marshaller接口,将Java对象序列化为XML数据。 Unmarshaller接口,将XML数据反序列化为Java对象。JDK中JAXB相关的重要Annotation:
@XmlType,将Java类或枚举类型映射到XML模式类型 @XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标注)字段到XML。其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。 @XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。 @XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML。 @XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。 @XmlRootElement,将Java类或枚举类型映射到XML元素。 @XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。 @XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。其他:
对于要序列化(marshal)为XML的Java类,绝不能把成员变量声明为public,否则运行将抛出异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException。 对于JAXB相关的重要Annotation的声明,如@Xml.....,可以放在成员变量的setter()或getter()方法上,两者中任选其一即可,但决不能放在成员变量上,否则运行将抛出异常com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException。[代码] Customer.java
01
package
com.mkyong.core;
02
03
import
javax.xml.bind.annotation.XmlAttribute;
04
import
javax.xml.bind.annotation.XmlElement;
05
import
javax.xml.bind.annotation.XmlRootElement;
06
07
@XmlRootElement
08
public
class
Customer {
09
10
String name;
11
int
age;
12
int
id;
13
14
public
String getName() {
15
return
name;
16
}
17
18
@XmlElement
19
public
void
setName(String name) {
20
this
.name = name;
21
}
22
23
public
int
getAge() {
24
return
age;
25
}
26
27
@XmlElement
28
public
void
setAge(
int
age) {
29
this
.age = age;
30
}
31
32
public
int
getId() {
33
return
id;
34
}
35
36
@XmlAttribute
37
public
void
setId(
int
id) {
38
this
.id = id;
39
}
40
41
}
[代码]将对象转为XML
01
package
com.mkyong.core;
02
03
import
java.io.File;
04
import
javax.xml.bind.JAXBContext;
05
import
javax.xml.bind.JAXBException;
06
import
javax.xml.bind.Marshaller;
07
08
public
class
JAXBExample {
09
public
static
void
main(String[] args) {
10
11
Customer customer =
new
Customer();
12
customer.setId(
100
);
13
customer.setName(
"mkyong"
);
14
customer.setAge(
29
);
15
16
try
{
17
18
File file =
new
File(
"C:\\file.xml"
);
19
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.
class
);
20
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
21
22
// output pretty printed
23
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
true
);
24
25
jaxbMarshaller.marshal(customer, file);
26
jaxbMarshaller.marshal(customer, System.out);
27
28
}
catch
(JAXBException e) {
29
e.printStackTrace();
30
}
31
32
}
33
}
[代码] 输出
1
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
standalone
=
"yes"
?>
2
<
customer
id
=
"100"
>
3
<
age
>29</
age
>
4
<
name
>mkyong</
name
>
5
</
customer
>
[代码] 将XML转为对象
01
package
com.mkyong.core;
02
03
import
java.io.File;
04
import
javax.xml.bind.JAXBContext;
05
import
javax.xml.bind.JAXBException;
06
import
javax.xml.bind.Unmarshaller;
07
08
public
class
JAXBExample {
09
public
static
void
main(String[] args) {
10
11
try
{
12
13
File file =
new
File(
"C:\\file.xml"
);
14
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.
class
);
15
16
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
17
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
18
System.out.println(customer);
19
20
}
catch
(JAXBException e) {
21
e.printStackTrace();
22
}
23
24
}
25
}
[代码] 输出
1
Customer [name=mkyong, age=
29
, id=
100
]
- JAXB
- jaxb
- JAXB
- JAXB
- jaxb
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- JAXB
- 从客户端检测到有潜在危险的Request.Form值
- 《那些年啊,那些事——一个程序员的奋斗史》——35
- 《那些年啊,那些事——一个程序员的奋斗史》——36
- cookie
- jsp相关知识
- JAXB
- Windows下新建多级文件夹
- SQL SERVER中CASE的一个实际例子
- 《那些年啊,那些事——一个程序员的奋斗史》——37
- 一个Fedora启动问题的解决
- MySQL中group_concat函数
- NET Framework 4.0-RequestValidationMode
- 关于发布《计算机信息系统集成企业资质等级评定条件(2012年修定版)》的通知 工信计资[2012]6号
- 【转】HTML - embed与object之争(转)