六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 56|回复: 0

JAXB如何Marshal父类?

[复制链接]

升级  74%

9

主题

9

主题

9

主题

童生

Rank: 1

积分
37
 楼主| 发表于 2013-1-15 03:14:54 | 显示全部楼层 |阅读模式
<link rel="stylesheet" type="text/css" href="http://maomaode.bokeland.com/plugins/plogeshi/styles/plogeshi.css">假设我们有这样的Schema:

<complexType name="Meta">       <sequence>                          <element name="name" type="string"/"">           <element name="description" type="string"/"">       </sequence>   </complexType>    <complexType name="Store">       <complexContent>           <extension base="Meta">               <sequence>                   <element name="books" type="Book" maxoccurs="unbounded"/"">               </sequence>           </extension>       </complexContent>   </complexType>    <element name="store" type="Store"/"">   <element name="meta" type="Meta"/"">  注意到Store 继承了Meta, 如果我们有一个Store的对象,但是我们只想Marshal出继承Meta部分的内容,即希望得到下面这样XML

<meta><name>my book store </name><description> abc </description></meta>  应该怎么写呢?答案其实就在于你怎么构建JAXBContext

JAXBElement jaxbElement =     (JAXBElement)m.invoke(objFactory.newInstance(), store); JAXBContext jc = JAXBContext.newInstance(Meta.class);Marshaller marshaller = jc.createMarshaller();marshaller.marshal(jaxbElement, os);  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表