Question

Now, I have two Java classes like this.

public class HogeDomain {

    private User userDomain;

    public HogeDomain() {
    }

         and getter/setter..  
}

public class User {

    public User() {
    }

    private String id;
    private String password;
    private Date userDate;

    and getter/setter..
}

And then, I created a xml schema above for using "Schemagen" of an Ant task automatically.

It's this.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="hogeDomain">
    <xs:sequence>
     <xs:element name="userDomain" type="user" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="user">
    <xs:sequence>
      <xs:element name="id" type="xs:string" minOccurs="0"/>
      <xs:element name="password" type="xs:string" minOccurs="0"/>
      <xs:element name="userDate" type="xs:dateTime" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

But I really want to create a xml schema like this to using JAXB marshalling or unmarshalling.

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:element name="hogeDomain">
      <xs:complexType>
         <xs:sequence>
           <xs:element ref="userDomain" minOccurs="0"/>
         </xs:sequence>
      </xs:complexType>
      </xs:element>

      <xs:element name="userDomain">
         <xs:complexType>
           <xs:sequence>
              <xs:element name="id" type="xs:string" minOccurs="0"/>
              <xs:element name="password" type="xs:string" minOccurs="0"/>
              <xs:element name="userDate" type="xs:dateTime" minOccurs="0"/>
           </xs:sequence>
         </xs:complexType>
      </xs:element>
 </xs:schema>

How to create this xml schema for using a "Schemagen" Ant task ? I don't want to write a xml schema for hand-made.

And is there any solutions when if it can't ?

Was it helpful?

Solution

You can do the following:

  1. You will need to add @XmlRootElement to each of you classes to get to cause global elements to be generated.
  2. You will need to add @XmlType(name="") to force the complex type definitions to be anonymous.
  3. You will need to use @XmlElementRef on property.

As demonstrated by:

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(name="")
public class HogeDomain {

    private User userDomain;

    @XmlElementRef
    public User getUserDomain() {
        return userDomain;
    }

    public void setUserDomain(User userDomain) {
        this.userDomain = userDomain;
    }

}

and

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(name="")
public class User {

}

To generate the following schema:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="hogeDomain">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="user"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="user">
    <xs:complexType>
      <xs:sequence/>
    </xs:complexType>
  </xs:element>
</xs:schema>

With the following code:

import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(HogeDomain.class);
        jc.generateSchema(new MySOR());

    }

    private static class MySOR extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top