如何在不使用命令提示符的情况下使用xjc

时间:2013-04-23 10:24:12

标签: jaxb xjc

    Hi friends,

    I want to generate Jaxb pojo classes using xjc by java code not by using command prompt how i will use it.

public static void main(String [] args) throws FileNotFoundException
    {
        try
        {
            JAXBContext jc = JAXBContext.newInstance(new Class[] {com.bcbsks.testjb.Report.class});             
            Unmarshaller um = jc.createUnmarshaller();          
            Report myJAXBObject = (Report)um.unmarshal(new java.io.FileInputStream("report.xsd"));
        } 
        catch( UnmarshalException ue ) 
        {    
            ue.printStackTrace(); 
        } 
        catch( JAXBException je ) 
        { 
            je.printStackTrace(); 
        } 
    }

我知道给定代码是错误的,但我想使用任何其他代码来生成pojo类。

1 个答案:

答案 0 :(得分:0)

尝试:

com.sun.tools.xjc.Driver.run(String[], XJCListener)

String[]是传递给XJC命​​令的参数,XJCListener的实现类似于以下内容:

public class Listener extends XJCListener {

    private ConsoleErrorReporter cer = new ConsoleErrorReporter(System.err);
    private String generatedPackagePath = null;

    public void generatedFile(String fileName, int count, int total) {
        message(fileName);
        if (this.generatedPackagePath == null) {
            this.generatedPackagePath = fileName.substring(0, fileName.lastIndexOf(File.separator));
        }
    }

    public String getGeneratedPackagePath() {
        return generatedPackagePath;
    }

    public void message(String msg) {
        System.out.println(msg);
    }

    public void error(SAXParseException exception) {
        cer.error(exception);
    }

    public void fatalError(SAXParseException exception) {
        cer.fatalError(exception);
    }

    public void warning(SAXParseException exception) {
        cer.warning(exception);
    }

    public void info(SAXParseException exception) {
        cer.info(exception);
    }

}