我正在尝试使用Java以编程方式从XSD文件生成JAXB类.我使用以下代码片段来实现:
....
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 static void generateJaxb(String schemaPath,
String outputDirectory,
String packageName) throws DataLoadingException
{
try {
// Setup schema compiler
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName(packageName);
// Setup SAX InputSource
File schemaFile = new File(schemaPath);
InputSource is = new InputSource(schemaFile.toURI().toString());
// Parse & build
sc.parseSchema(is);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(new File(outputDirectory));
} catch (IOException exec) {
LOGGER.error("Error while generating JAXB …Run Code Online (Sandbox Code Playgroud) 我有许多由JAXB的xsd2java生成的类.我需要在编译时使用特定注释对所有这些类进行注释(例如使用lombok注释).有没有办法做到这一点,例如一些代码生成工具?
我想在生成“Doc”类之前生成“@java.lang.SuppressWarnings("all")”。
问题:jaxb2-annotate-plugin 不生成注释。
我的 pom.xml:
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<extension>true</extension>
<arguments>-Xannotate</arguments>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.4</version>
</dependency>
<dependency>
<groupId>com.sun.codemodel</groupId>
<artifactId>codemodel</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.4</version>
</dependency>
<dependency>
<groupId>com.sun.codemodel</groupId>
<artifactId>codemodel</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId> …Run Code Online (Sandbox Code Playgroud) 我目前有这段代码:
Map<Site, LinkedList<Site.Hosts.Host>> map = new HashMap<Site, LinkedList<Site.Hosts.Host>>();
for (PerformanceCounter element : pc) {
Site s = new Site();
s.id = Short.parseShort(element.getSite_id());
s.name = element.getSite_name();
s.location = element.getSite_location();
Site.Hosts.Host h = new Site.Hosts.Host();
h.id = Short.parseShort(element.getHost_id());
if (!map.containsKey(s)) {
map.put(s, new LinkedList<Site.Hosts.Host>());
} else {
map.get(s).add(h);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在阅读的 PerformanceCounters 列表是:
1, C-01, New York, 1001
1, C-01, New York, 1002
1, C-01, New York, 1003
Run Code Online (Sandbox Code Playgroud)
其中 101 是 ID,C-01 是名称,纽约是位置。
不幸的是,我的代码为我的地图创建了 3 个键值。
我无法生成 1 个键和 3 个值LinkedList …