JAXB - The JAXB Context

本文介绍了如何通过包名列表或指定类来创建JAXBContext实例。解释了使用ObjectFactory类或jaxb.index文件的方式,并提供了包含类路径的示例。此外还说明了JAXB如何处理静态引用和忽略子类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As we have seen, an object of the class JAXBContext must be constructed as a starting point for other operations. One way is to create a context from a colon separated list of packages.

JAXBContext ctxt = JAXBContext.newInstance( "some foo:more.bar" );

Each package must contain its own class ObjectFactory or a file named jaxb.index. An ObjectFactory class is generated by the XML schema compiler, and therefore this form of newInstance is usually used in connection with schema derived classes. For JAXB annotated Java code, you may use the package path form as well, either with a hand-written ObjectFactory class or with a jaxb.index resource file containing a list of the class names to be considered by JAXB. This file simply lists the class names relative to the package where it occurs, e.g.:

# package some.foo
# class some.foo.Foo
Foo
# inner class some.foo.Foo.Boo
Foo.Boo

An alternative form of the newInstance method lists all classes that the new context should recognize.

JAXBContext ctxt = JAXBContext.newInstance( Foo.class, Bar.class );

Usually, the top level classes are sufficient since JAXB will follow, recursively, all static references, such as the types of instance variables. Subclasses, however, are not included.

If packages or classes are associated with namespaces, the packages or classes associated with a JAXB context also determine the namespace declarations written as attributes into the top-level element of the generated XML document.

 

转载于:https://www.cnblogs.com/huey/p/5511196.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值