This discussion is archived
3 Replies Latest reply: Aug 22, 2011 6:44 AM by 355996 RSS

(JAXB) XJC compiles schema but output does not compile. (Recursion)

800281 Newbie
Currently Being Moderated
Hello,

I am new to JAXB but decided to give it a try.
I have a nested schema like this:
...
  <xs:element name="topic">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="topic" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="topic" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="topic" minOccurs="0" maxOccurs="unbounded">
...
And when xjc compiled I get Java code which don't compile.

Example in Topic.java
public class Topic {
    ...
   public static class Topic {
       ...
    }
}
with the error "+The nested type Topic cannot hide an enclosing type+"

The problem is obviously that xjc creates nested classes with the same name as the parent class.
The XML Schema works fine in reqular XML editors.
Is it so that xjc only works with tailored schemas and I must read up on its limitation or is xjc intended to work with all valid schemas?
In either case do someone have any tips or thoughts on this.


/Thanks Farmor
  • 1. Re: (JAXB) XJC compiles schema but output does not compile. (Recursion)
    843834 Newbie
    Currently Being Moderated
    hi Farmor,

    did you able to resolve your problem? I am facing similar problem.
    I am trying to create

    <compare>
    <condition>
         <conditions>
              <condition>
                   <negated>N</negated>
                   <leftvalue>rownum</leftvalue>
                   <function>></function>
                   <rightvalue/>
                   <value>
                             <name>constant</name>
                             <type>Integer</type>
                             <text>1</text>
                             <length>-1</length>
                             <precision>0</precision>
                             <isnull>N</isnull>
                             <mask>#;-#</mask>
                        </value>
              </condition>
         <conditions>
    </condition>
    </compare>



    can you please help?

    Thanks
    Vishal
  • 2. Re: (JAXB) XJC compiles schema but output does not compile. (Recursion)
    828583 Newbie
    Currently Being Moderated
    Hi,

    Exact same problem here... with this XML having different elements, with the same name:

    <root>
    <mytype>
    <test>
    <mytype>
    ...
    </mytype>
    </test>
    </mytype>
    <root>

    Since the two "mytype" elements are different but have the same name, XJC will fail to build a compilable class due to two nested classes of type "mytype". Moreover, there's no namespace used, and it's a web service we don't control.

    I was hoping it was a known issue, but cannot find any documentation on this. Anybody has a solution?

    Thanks,

    Daniel
  • 3. Re: (JAXB) XJC compiles schema but output does not compile. (Recursion)
    355996 Newbie
    Currently Being Moderated
    You can workaround this issue by using an external bindings file:

    recursive.xml

    The binding file allows you to rename the nested classes to avoid the name conflict.
        <jaxb:bindings 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            version="2.1">
            <jaxb:bindings schemaLocation="recursive.xsd">
                <jaxb:bindings node="/xsd:schema/xsd:element[@name='topic']/xsd:complexType/xsd:sequence/xsd:element[@name='topic']/xsd:complexType">
                    <jaxb:class name="Topic2"/>
                </jaxb:bindings>
                <jaxb:bindings node="/xsd:schema/xsd:element[@name='topic']/xsd:complexType/xsd:sequence/xsd:element[@name='topic']/xsd:complexType/xsd:sequence/xsd:element[@name='topic']/xsd:complexType">
                    <jaxb:class name="Topic3"/>
                </jaxb:bindings>
            </jaxb:bindings>
        </jaxb:bindings>
    recursive.xsd
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:element name="topic">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="topic" minOccurs="1" maxOccurs="1">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="topic" minOccurs="1" maxOccurs="1">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="topic" minOccurs="0" maxOccurs="unbounded" type="xs:string"/>
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>
                    </xs:sequence>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:schema>
    XJC call

    The binding file is specified in the XJC call:
        xjc -d out -b recursive.xml recursive.xsd
    Topic (Generated Class)
        package generated;
        
        import java.util.ArrayList;
        import java.util.List;
        import javax.xml.bind.annotation.XmlAccessType;
        import javax.xml.bind.annotation.XmlAccessorType;
        import javax.xml.bind.annotation.XmlElement;
        import javax.xml.bind.annotation.XmlRootElement;
        import javax.xml.bind.annotation.XmlType;
        
        
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "topic"
        })
        @XmlRootElement(name = "topic")
        public class Topic {
        
            @XmlElement(required = true)
            protected Topic.Topic2 topic;
        
            public Topic.Topic2 getTopic() {
                return topic;
            }
        
            public void setTopic(Topic.Topic2 value) {
                this.topic = value;
            }
        
            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
                "topic"
            })
            public static class Topic2 {
        
                @XmlElement(required = true)
                protected Topic.Topic2 .Topic3 topic;
        
                public Topic.Topic2 .Topic3 getTopic() {
                    return topic;
                }
        
                public void setTopic(Topic.Topic2 .Topic3 value) {
                    this.topic = value;
                }
        
                @XmlAccessorType(XmlAccessType.FIELD)
                @XmlType(name = "", propOrder = {
                    "topic"
                })
                public static class Topic3 {
        
                    protected List<String> topic;
        
                    public List<String> getTopic() {
                        if (topic == null) {
                            topic = new ArrayList<String>();
                        }
                        return this.topic;
                    }
        
                }
        
            }
        
        }