JAXB没有为包含生成的Java类的类生成所需的模式?

时间:2013-03-12 14:39:49

标签: java xml xsd jaxb

我有这个班级

@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;
    }
}

当我使用此代码生成架构时:

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;
           }
        }

}

我得到的只是GpxType类的XSD。那是为什么?

只是为了通知您,GpxType和JourneyXML是从XSD文件生成的。

如何强制执行此操作以生成我需要的相应XSD?

1 个答案:

答案 0 :(得分:1)

尝试将createOutput方法更改为不总是写入request.xsd文件。我相信您的模型中有多个名称空间,因此正在生成多个XML模式。

@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;
   }