JAXB 对根元素感到困惑?
我正在尝试使用 Visio XML 架构封送文件,由3个模式文件组成,使用XJC生成java源时会生成三个包:
- com.microsoft.schemas.visio._2003.core
- com.microsoft.schemas.visio._2006.extension
- com.microsoft.schemas.office.visio._2010.extension
根元素是 VisioDocument
,我使用的所有类都在 <代码>2003包。
以下是我编组 XML 文件的方法:
VisioDocumentType visioDoc = new VisioDocumentType();
... manipulated here ...
JAXBContext jc = JAXBContext.newInstance("com.microsoft.schemas.visio._2003.core");
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(new JAXBElement<VisioDocumentType>(new QName("uri","local"), VisioDocumentType.class, visioDoc), bw);
执行时,我收到此错误:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.microsoft.schemas.visio._2003.core.PagePropsType" as an element because it is missing an @XmlRootElement annotation]
我正在使用 PagePropsType
,但它不是根元素。 JAXB 为什么这么认为?
I'm trying to marshal a file using the Visio XML Schema, which consists of 3 schema files and produces three packages when java source is generated with XJC:
- com.microsoft.schemas.visio._2003.core
- com.microsoft.schemas.visio._2006.extension
- com.microsoft.schemas.office.visio._2010.extension
The root element is VisioDocument
, and all of the classes I'm using are in the 2003
package.
Here is my approach to marshalling my XML file:
VisioDocumentType visioDoc = new VisioDocumentType();
... manipulated here ...
JAXBContext jc = JAXBContext.newInstance("com.microsoft.schemas.visio._2003.core");
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(new JAXBElement<VisioDocumentType>(new QName("uri","local"), VisioDocumentType.class, visioDoc), bw);
When executed, I receive this error:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.microsoft.schemas.visio._2003.core.PagePropsType" as an element because it is missing an @XmlRootElement annotation]
I am using PagePropsType
, but it is not a root element. Why does JAXB think it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在代码的
... 这里操作 ...
部分。基于您执行以下操作(或类似操作)的假设。
(
ShapeSheetType
是StyleSheetType
等的超类。)如果是这种情况,那么您的问题在于添加
pageProps< /code> 直接添加到列表。
如果您查看 getTextOrXFormOrLine() 方法的文档,它会列出列表可以保存的类型。每种类型都包装在
JAXBElement<...>
中,因此您必须在将其添加到列表之前包装pageProps
。你应该这样做:
(请注意,我使用 XJC 2.2.4 来编译模式;对我来说,每个类的名称都以
Type
为后缀。也许这就是我结束的原因像您一样使用VisioDocumentType
而不是VisioDocument
,但这应该不重要。)The problem resides in the
... manipulated here ...
part of your code.Based on the assumption that you do the following (or something similar).
(
ShapeSheetType
is a superclass forStyleSheetType
, et cetera.)If this's the case, then your problem lies in adding the
pageProps
to the list directly.If you take a look at the
getTextOrXFormOrLine()
method's documentation it lists what kind of types the list can hold. Every type is wrapped in aJAXBElement<...>
so you have to wrappageProps
before adding it to the list.You should do it like this:
(Note that I've used XJC 2.2.4 to compile the schemas; for me every class' name is suffixed with
Type
. Maybe this is why I ended up withVisioDocumentType
instead ofVisioDocument
like you, but it shouldn't matter.)如果检查生成的代码,您将在其中找到一个
ObjectFactory
类。此类应具有一个方法,该方法返回包装在JAXBElement
中的VisioDocument
,并且它是要传递给编组器的对象。同样适用于您在
VisioDocument
中设置的所有对象 - 不要使用“new
”创建它们,而是使用ObjectFactory
创建它们。If you check your generated code, you will find a
ObjectFactory
class in there. This class should have a method that returns aVisioDocument
wrapped in aJAXBElement
, and that it the object that you want to pass to the marshaller.Same applicable to all objects you set inside
VisioDocument
- don't create them with 'new
' but useObjectFactory
.