XJC없이 JAXB 스키마 파일에서 Java 코드를 생성 한 사람은 누구입니까? 동적으로 즉석에서 자바 코드를 컴파일하는 데 사용Java 소스 (xjc없이)를 동적으로 생성
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler()
다소 유사
.
참고 : JDK 6에서 실행 com.sun.*
도구 패키지 (힌트 주셔서 감사합니다 Blaise Doughan)
XJC없이 JAXB 스키마 파일에서 Java 코드를 생성 한 사람은 누구입니까? 동적으로 즉석에서 자바 코드를 컴파일하는 데 사용Java 소스 (xjc없이)를 동적으로 생성
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler()
다소 유사
.
참고 : JDK 6에서 실행 com.sun.*
도구 패키지 (힌트 주셔서 감사합니다 Blaise Doughan)
I을 독립 실행 형 JDK 6은 xjc 유틸리티 클래스에 대한 액세스를 제공하지 않기 때문에 해결책을위한 일부 J2EE 라이브러리를 포함해야했습니다.
import com.sun.codemodel.*;
import com.sun.tools.xjc.api.*;
import org.xml.sax.InputSource;
// Configure sources & output
String schemaPath = "path/to/schema.xsd";
String outputDirectory = "schema/output/source/";
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("com.xyz.schema.generated");
// Setup SAX InputSource
File schemaFile = new File(schemaPath);
InputSource is = new InputSource(new FileInputStream(schemaFile));
is.setSystemId(schemaFile.getAbsolutePath());
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(new File(outputDirectory));
* 된 .java 소스는 JAXB 참조 구현 here 받기 outputDirectory
이 다음은 도움이되지 않습니다 것을 의미 :
에 배치됩니다.
Java 코드를 생성 할 수있는 com.sun.tools.xjc.api.XJC 클래스가 포함되어 있습니다.
이 코드는 특정 디렉토리에 파일을 생성/패키지 구조 :
import java.io.File;
import java.io.IOException;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
public class JAXCodeGen {
public static void main(String[] args) throws IOException {
String outputDirectory = "E:/HEAD/JAXB/src/";
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("com.xyz.schema");
// Setup SAX InputSource
File schemaFile = new File("Item.xsd");
InputSource is = new InputSource(schemaFile.toURI().toString());
// is.setSystemId(schemaFile.getAbsolutePath());
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(new File(outputDirectory));
}
}
메이븐의 의존성을 얻기의 또 다른 방법;
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2.11</version>
</dependency>
parseSchema : java.lang.IllegalArgumentException : 시스템 ID 'C : \ Users \ Federico \ Documents \ NetBeansProjects \ Wadl2Java \ myxsd.xsd '는 절대적이지 않습니다.'제발 도와 줄 수 있어요? –
로컬 파일의 경우 절대 경로를 사용하고 시스템 ID를 'is.setSystemId (schemaFile.toURI(). toString())'로 설정했습니다. –
@Uccio, 미안, 휴가 중이었기 때문에 문제가 해결되었습니다. – Osw