Java 理解 JAXB @XmlRootElement 注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16584555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:34:32  来源:igfitidea点击:

Understanding JAXB @XmlRootElement annotation

javaxmlxsdjaxbmarshalling

提问by Little Child

I am using the tutorial herefor understanding JAXB.

我在这里使用教程来理解 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 {
       ...
}  

Although I will be manually generating my classes rather than letting Eclipse do it, I will supply an XSDwith 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.

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

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

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

采纳答案by bdoughan

I recommend using the package level @XmlSchemaannotation to specify the namespace qualification for you model. A package level annotation goes in a special class called package-infothat 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.

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

org/example/foo/package-info.java

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;

Overriding the Namespace

覆盖命名空间

  • You can override the namespace given in the @XmlSchemafor all properties in a class using the @XmlTypeannotation.
  • You can override the namespace for a given element using the namespace property on the @XmlRootElementor @XmlElementannotation.
  • 您可以@XmlSchema使用@XmlType注释覆盖在类中的所有属性中给出的命名空间。
  • 您可以使用@XmlRootElementor@XmlElement注释上的命名空间属性覆盖给定元素的命名空间。

For More Information

想要查询更多的信息

回答by Krutik

  • @XmlRootElement annotation can be used to map a class or enum type to XML type.

  • When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.

  • Follow the example given below to get more idea:

  • @XmlRootElement 注释可用于将类或枚举类型映射到 XML 类型。

  • 当顶级类或枚举类型使用 @XmlRootElement 注释进行注释时,其值在 XML 文档中表示为 XML 元素。

  • 按照下面给出的示例获得更多想法:

Associate an element with XML Schema type

将元素与 XML 架构类型相关联

// Example: Code fragment
 @XmlRootElement
 class Point {
    int x;
    int y;
    Point(int _x,int _y) {x=_x;y=_y;}
 }

 //Example: Code fragment corresponding to XML output
 marshal( new Point(3,5), System.out);


 <!-- Example: XML output -->
 <point>
   <x> 3 </x>
   <y> 5 </y>
 </point>