How to use JAXB APIs to generate classes from xsd?

后端 未结 2 1207
南笙
南笙 2021-01-23 22:14

I need to generate bean classes from .xsd without using xjc command or ant. i have found the implementation in Apache Axis2 but i am unable to generate the artifacts.

i

2条回答
  •  攒了一身酷
    2021-01-23 23:17

        SchemaCompiler sc = XJC.createSchemaCompiler();
        File file = null;
        file = new File("Item.xsd");
        sc.parseSchema(new InputSource(file.toURI().toString()));
        S2JJAXBModel model = sc.bind();
    
        JCodeModel cm = model.generateCode(null, null);
        cm.build(new File("."));
    

    //I am having Item.xsd in local directory and this code has generated classes. If i remove this xsd locally will get null pointer exception. Also note i am having my jaxb related jars in Java Build path of eclipse project.

提交回复
热议问题