1
    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 类。

4

1 回答 1

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

}
于 2013-04-23T21:10:40.893 回答