Understanding JAXB @XmlRootElement annotation
Asked Answered
C

2

15

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 {
       ...
}  

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.

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") ?

Connacht answered 16/5, 2013 at 10:6 Comment(0)
T
24

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;

Overriding the Namespace

  • 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.

For More Information

Tyro answered 16/5, 2013 at 10:19 Comment(7)
Ah I understand what you are saying.... for a package which contains the various XML-to-Java classes, I must create class package-info and modify the above content as needed ? Cool!. I will look into what you said but what about my original question ? :) Was my inference correctConnacht
@LittleChild - You will get an XML schema with that has de.vogella.xml.jaxb.model as the target namespace that defines the bookstore element. The problem is that the class won't inherit the namespace specified in the @XmlRootElement annotation so they would generate into another XML schema.Tyro
basically because elementFormDefault is not set to qualified ? and oh, schema will also be hand written.Connacht
@LittleChild - No. The namespace specified on the @XmlRootElement annotation only ever applies to that element. The namespace parameter specified on the @XmlType annotation applies to the properties in the class based on the elementFormDefault specified on the @XmlSchema annotation.Tyro
ah I get your point. only <bookstore> tag will belong to de.vogella.xml.jaxb.model and <books> won't with @XmlRootElement . Would it be too much to ask if I emailed you my XML and XSD and asked you to make POJO class out of it ?Connacht
@LittleChild - I won't do that, but since you have an XML Schema why don't you generate your model using the XJC tool from your JDK install? <JDK_HOME>/bin/xjc yourSchema.xsdTyro
Well, I made a JAR file so that I could try some JAXB. So I first decided to validate. It did not validate. More news here: #16616273Connacht
M
7
  • @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:

Associate an element with XML Schema type

// 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>
Marvelofperu answered 5/2, 2016 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.