简体   繁体   中英

JAXB is not generating the required schema for a class that contains generated Java classes?

I have this class

@XmlRootElement
public class GpsDataRequest{

    //definition of variables



    @XmlElement(required=true, type=GpxType.class)
    public GpxType getGpxRoot() {
        return gpxRoot;
    }

    @XmlElement(required=true, type=JourneyXML.class)
    public JourneyXML getJourneyPlanRoot() {
        return journeyPlanRoot;
    }

    @XmlElement(required=true)
    public String getSecurityToken() {
        return securityToken;
    }

    @XmlElement(required=true)
    public UUID getUuid() {
        return uuid;
    }
}

When i generate the schema using this code:

public class SchemaGenerator {

    public static void main(String[] args)
    {
        try {
            JAXBContext context=
                    JAXBContext.newInstance(GpsDataRequest.class);
            SchemaOutputResolver sor =new DemoSchemaOutputResolver();
            context.generateSchema(sor);

        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public static class DemoSchemaOutputResolver extends SchemaOutputResolver {

           @Override
           public Result createOutput(String namespaceUri, String suggestedFileName)
                 throws IOException {

              // create new file
              File file = new File("request.xsd");

              // create stream result
              StreamResult result = new StreamResult(file);

              // set system id
              result.setSystemId(file.toURI().toURL().toString());

              // return result
              return result;
           }
        }

}

All I get is the XSD of the GpxType class only. Why is that?

Just to let you know, GpxType and JourneyXML are generated from XSD files.

How to enforce this to generate the appropriate XSD I need?

Try changing your createOutput method to not always write to the request.xsd file. I believe there are multiple namespaces in your model and therefore multiple XML schemas are being generated.

@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {

      // create new file
      File file = new File(suggestedFileName);

      // create stream result
      StreamResult result = new StreamResult(file);

      // set system id
      result.setSystemId(file.toURI().toURL().toString());

      // return result
      return result;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM