使用JAXB从Java bean或XML创建XSD

时间:2013-10-07 06:18:22

标签: java xml jaxb xsd

我正在使用此代码

JAXBContext jc = JAXBContext.newInstance(Bookdata.class);
Bookdata bookdata=new Bookdata();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(bookdata, (OutputStream) output);

但它正在生成XML但我需要XSD 我需要像这样创建XSD:

<ArrayOfCommandInfoDTO>
     <CommandInfoDTO>
        <a:allowAddingGameCenterFriends>true</a:allowAddingGameCenterFriends>
        <a:enter code here>allowAppInstallation>true</a:allowAppInstallation>
        <a:allowAssistant>true</a:allowAssistant>
        <a:allowAssistantWhileLocked>true</a:allowAssistantWhileLocked>
        <a:allowCamera>true</a:allowCamera>
     </CommandInfoDTO>
</ArrayOfCommandInfoDTO>

所以请告诉我如何从java Beans或XML

创建XSD

2 个答案:

答案 0 :(得分:1)

public class ObjToSchema {

    public static void main(String[] args) throws IOException, JAXBException {
        // TODO Auto-generated method stub
        (JAXBContext.newInstance(Bookdata.class)).generateSchema(new DataSchemaOutputResolver());
    }
}


public class DataSchemaOutputResolver extends SchemaOutputResolver {
    @Override
    public Result createOutput(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub
        return new StreamResult(new File("d:/data.xsd"));
    }
}

希望这能解决您的问题。

答案 1 :(得分:0)

您可以使用generateSchema上的JAXBContext方法生成XML架构。