如何从xsd

时间:2017-06-12 13:06:16

标签: java xml serialization xsd

我必须生成一个实现可序列化接口的类,以下是我正在使用的xsd文件,也使用maven jaxb2插件

这是我的.xsd文件

<?xml version="1.0" encoding="windows-1252" ?>

<xsd:complexType name="Status">
    <xsd:sequence>
        <xsd:element name="startTime" type="xsd:double" minOccurs="0" />
        <xsd:element name="endTime" type="xsd:double" minOccurs="0" />
    </xsd:sequence>
</xsd:complexType>

我的JAXB2 plugin

 <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <executions>
                <execution>
                    <id>status</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <forceRegenerate>true</forceRegenerate>
                        <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                        <schemaIncludes>
                            <include>Status.xsd</include>
                        </schemaIncludes>
                        <generatePackage>com.test.model</generatePackage>
                        <args>
                            <arg>-XtoString</arg>
                            <arg>-Xcopyable</arg>
                            <arg>-Xequals</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics</artifactId>
                                <version>0.6.4</version>
                            </plugin>
                        </plugins>
                    </configuration>
                </execution>

            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.datastax.cassandra</groupId>
                    <artifactId>cassandra-driver-core</artifactId>
                    <version>3.1.3</version>
                </dependency>
                <dependency>
                    <groupId>com.datastax.cassandra</groupId>
                    <artifactId>cassandra-driver-mapping</artifactId>
                    <version>3.1.3</version>
                </dependency>
            </dependencies>
        </plugin>

这里生成了实现Cloneable, CopyTo, Equals, ToString interfaces的java类我尝试使用<arg>-Xserializable</arg>但它没有用,我需要这个生成的类也应该实现Serializable interface

2 个答案:

答案 0 :(得分:0)

我使用了像这样的jaxb2-maven-plugin:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extension>true</extension>
                <arguments>-XtoString -Xcopyable</arguments>
                <outputDirectory>target/generated-sources/</outputDirectory>
                <schemaDirectory>src/main/resources</schemaDirectory>
                <bindingDirectory>src/main/resources</bindingDirectory>
                <bindingFiles>binding.xml</bindingFiles>
            </configuration>
        </plugin>

binding.xml:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">
<jaxb:globalBindings generateIsSetMethod="true">
    <xjc:serializable uid="12343" />
</jaxb:globalBindings>

有了这种依赖:

<dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-basics</artifactId>
</dependency>

答案 1 :(得分:0)

我得到了解决方案 已修改xsd:schema declaration to

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
       xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
       jaxb:extensionBindingPrefixes="xjc"
       jaxb:version="1.0">
 <xsd:annotation>
   <xsd:appinfo>
    <jaxb:globalBindings generateIsSetMethod="true">
      <xjc:serializable uid="12343"/>
    </jaxb:globalBindings>
  </xsd:appinfo>
</xsd:annotation>

然后在jaxp.properties下添加path/to/jdk1.8.0/jre/lib(如果它不存在),然后在其中写下这一行:

javax.xml.accessExternalSchema = all