了解JAXB @XmlRootElement批注 [英] Understanding JAXB @XmlRootElement annotation

查看:105
本文介绍了了解JAXB @XmlRootElement批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此处的教程来理解JAXB。

I am using the tutorial here for understanding JAXB.

当作者来创建文档的根目录时,作者开始如下:

When the writer comes to create the root of the document, the writer begins as below:

//This statement means that class "Bookstore.java" is the root-element of our example
@XmlRootElement(namespace = "de.vogella.xml.jaxb.model")
public class Bookstore {
       ...
}  

虽然我会要手动生成我的类,而不是让Eclipse这样做,我将提供一个 XSD 与我的jar文件(不在内部打包,而是在包含jar文件的文件夹中),这样当我的应用程序启动后,它将验证XML文档是否已被篡改。

Although I will be manually generating my classes rather than letting Eclipse do it, I will supply an XSD with my jar file (not packed inside but rather in the folder containing jar file) so that when my application starts, it will validate whether the XML document has been tampered with.

因此,在XSD文件中, targetNamespace 将是 de.vogella.xml .jaxb.model 因为它在上面声明为 @XmlRootElement(namespace =de.vogella.xml.jaxb.model)

So, in the XSD file, the targetNamespace will be de.vogella.xml.jaxb.model because it was declared above as @XmlRootElement(namespace = "de.vogella.xml.jaxb.model") ?

推荐答案

我建议使用包级别 @XmlSchema 注释来指定命名空间限定为你模型。包级别注释位于名为 package-info 的特殊类中,其中包含如下所示的确切内容。该注释将意味着文档中没有明确命名空间的所有元素都将使用该命名空间。

I recommend using the package level @XmlSchema annotation to specify the namespace qualification for you model. A package level annotation goes in a special class called package-info that contains the exact content as shown below. That annotation will mean that all elements in your document without an explicit namespace given will use that namespace.

org / example / foo / package-info.java

@XmlSchema(
    namespace = "http://www.example.org/foo",
    elementFormDefault = XmlNsForm.QUALIFIED)
package org.example.foo;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

覆盖命名空间


  • 您可以使用 @XmlType覆盖类中所有属性的 @XmlSchema 中给出的命名空间 annotation。

  • 您可以使用 @XmlRootElement 上的namespace属性覆盖给定元素的命名空间或 @XmlElement 注释。

  • You can override the namespace given in the @XmlSchema for all properties in a class using the @XmlType annotation.
  • You can override the namespace for a given element using the namespace property on the @XmlRootElement or @XmlElement annotation.

更多信息

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
  • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html

这篇关于了解JAXB @XmlRootElement批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆