Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:06:05+00:00 2026-05-28T00:06:05+00:00

I have some problem while trying to use the schemagen tool from JAXB library

  • 0

I have some problem while trying to use the schemagen tool from JAXB library to generate a Schema for my project. The problem is that the annotation @XmlAttribute is not being parsed properlly.

- src
 - teste
  - entity

So, the problem is that for some classes, the flag required in XmlAttribute is being ignored completly by the schemagen task.

I’ll paste here some examples of classes and the generated schema so you can understand what is going on

package teste.entity;

import javax.xml.bind.annotation.XmlAttribute;

public abstract class Class2 {

    @XmlAttribute(required=false)
    public String stringNotRequired;

    @XmlAttribute(required=true)
    public String stringRequired;

    @XmlAttribute(required=false)
    public int anotherIntNotRequired;

    @XmlAttribute(required=true)
    public int anotherIntRequired;

}

package teste.entity;

import javax.xml.bind.annotation.XmlAttribute;

public class Class1 extends Class2 {

    @XmlAttribute(required=true)
    public Integer integerRequired;

    @XmlAttribute(required=false)
    public Integer integerNotRequired;

    @XmlAttribute(required=false)
    public int intNotRequired;

    @XmlAttribute(required=true)
    public int intRequired;

    public Class1() {
    }
}

My package-info.java

@XmlSchema(xmlns = @XmlNs(prefix = "t", namespaceURI = "http://test.com"),
namespace = "http://test.com",
elementFormDefault = XmlNsForm.UNQUALIFIED,
attributeFormDefault = XmlNsForm.UNQUALIFIED)
package teste.entity;

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

When I run the schemagen task, I got this output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" version="1.0" targetNamespace="http://test.com" xmlns:t="http://test.com" xmlns:tns="http://test.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="class1">
    <xs:complexContent>
      <xs:extension base="tns:class2">
        <xs:sequence/>
        <xs:attribute name="integerRequired" type="xs:int" use="required"/>
        <xs:attribute name="integerNotRequired" type="xs:int"/>
        <xs:attribute name="intNotRequired" type="xs:int" use="required"/>
        <xs:attribute name="intRequired" type="xs:int" use="required"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="class2" abstract="true">
    <xs:sequence/>
    <xs:attribute name="stringNotRequired" type="xs:string"/>
    <xs:attribute name="stringRequired" type="xs:string" use="required"/>
    <xs:attribute name="anotherIntNotRequired" type="xs:int" use="required"/>
    <xs:attribute name="anotherIntRequired" type="xs:int" use="required"/>
  </xs:complexType>
</xs:schema>

Here is my Ant Task

<target name="generate-schema" >
    <path id="mycp">
        <fileset dir="lib/jaxb/lib\">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="lib" >
            <include name="*.jar"/>
        </fileset>
        <fileset dir="dist" >
            <include name="*.jar"/>
        </fileset>
    </path>

    <taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask">
        <classpath refid="mycp"/>
    </taskdef>

    <mkdir dir="schema"/>

    <schemagen srcdir="src/teste/entity" destdir="schema" >
        <classpath refid="mycp"/>
        <schema namespace="http://test.com" file="full.xsd" />            
    </schemagen>
</target>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:06:06+00:00Added an answer on May 28, 2026 at 12:06 am

    Do I need annotations on the classes for the namespace to get output correctly?

    Probably. I’ve created a setup similar to yours.

    Test.java

    package test;
    
    ...
    
    @XmlType(name = "test", namespace = "http://test.com", propOrder = "b")
    @XmlRootElement(name = "test", namespace = "http://test.com")
    public final class Test {
    
      @XmlAttribute(required = false)
      public String a;
    
      @XmlElement
      public String b;
    
      public Test() {}
    }
    

    package-info.java

    @XmlSchema(xmlns = @XmlNs(prefix = "tns", namespaceURI = "http://test.com"),
               namespace = "http://test.com",
               elementFormDefault = XmlNsForm.QUALIFIED,
               attributeFormDefault = XmlNsForm.QUALIFIED)
    package test;
    
    import javax.xml.bind.annotation.XmlNs;
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;
    

    build.xml (snippet, based on OP’s input)

    <schemagen srcdir="<path-to-test-package>" destdir=".">
      <classpath refid="<classpath-refid>" />
      <schema namespace="http://test.com" file="test.xsd" />
    </schemagen>
    

    test.xsd (output)

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xs:schema 
      attributeFormDefault="qualified"
      elementFormDefault="qualified" version="1.0"
      targetNamespace="http://test.com"
      xmlns:tns="http://test.com"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="test" type="tns:test"/>
    
      <xs:complexType name="test" final="extension restriction">
        <xs:sequence>
          <xs:element name="b" type="xs:string" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute ref="tns:a"/>
      </xs:complexType>
    
      <xs:attribute name="a" type="xs:string"/>
    </xs:schema>
    

    (I’ve used the prefix tns because JAXB will generate and use that instead of any prefix that was specified in @XmlNs.)

    Given the files above XJC’s SchemaGen produces the desired results—if I have a required attribute, then it’ll be generated as such; if I set required=false, then it won’t.

    The main thing here is the @XmlType annotation on the Test class. You just can’t live without that if you handcraft your classes. (The @XmlRootElement isn’t necessary, so it depends on your use-case if you want it or not.) This tells JAXB which namespace does the Test class (which represents a schema type by the way) belongs when SchemaGen processes it.

    The package-info.java serves almost the same purpose but at a schema level. If a package contains this file (and the annotations shown above) and SchemaGen encounters, then it’ll know that the classes (schema types) in that package belongs to the namespace specified in the package level annotations. (Again, if you doing things by hand this is a must.) Other than this, SchemaGen uses the namespace declared in this file to send the output to the file you specified in <schema namespace="http://test.com" file="test.xsd" />. Without it the generated file’s name is always schema1.xsd (or similar).

    Multiple packages

    If you want to group several classes located in multiple packages into one namespace, then you’ll have to apply the same package level annotations on all of the packages (like in the package-info.java snippet above—the namespace prefix used must be tns, because of the JAXB limitation described earlier).

    schemagen of course needs to be ordered to compile both packages. In your build.xml the schemagen task’s srcdir should encompass both packages.

    If you have a structure similar to this

    .
    |-- x
    |   |-- A.java             # JAXB
    |   |-- B.java             # POJO
    |   `-- package-info.java  # http://test.com
    `-- y
        |-- C.java             # JAXB
        `-- package-info.java  # http://test.com
    

    you can tell the schemagen task to compile only A and C like this

    <schemagen srcdir="." destdir="." >
      <schema namespace="http://test.com" file="test.xsd" />
      <include name="x/A.java" />
      <include name="y/C.java" />
    </schemagen>
    

    Optional and required attributes

    There is a short section in the official JAXB tutorial on attributes, which unfortunately doesn’t say a thing about primitive Java types and XML Schema types in particular.

    The shortcoming you’re experiencing isn’t a JAXB defect in my opinion, rather a Java oddity: primitive types can’t be null, which is good and bad at the same time.

    You can solve this by changing your primitive attribute

    @XmlAttribute
    public int attribute;
    
    @XmlAttribute
    public Integer attribute;
    

    (I’ve found a mailing list thread regarding this same issue, maybe you’re interested in it.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've encountered an curious problem while trying to use some objects through JSNI in
I have come across an annoying problem while writing some PHP4 code. I renamed
I've been into this problem for a while already, and have asked some questions
While doing some small regex task I came upon this problem. I have a
While trying to use LINQ to SQL I encountered several problems. I have table
I have run into a problem while writing C++ code that needs to compile
I'm trying to use java to read a string from a file that was
I have some trouble with python. I am trying to get output from a
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have some problem with Jquery Autocomplete plugin ( http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ ) I got it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.