2013-04-23 75 views
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类。如何在不使用命令提示符的情况下使用xjc

回答

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

} 
相关问题