如何从xsd生成实现可序列化的java文件

问题描述 投票:0回答:3

我必须生成一个也实现可序列化接口的类,以下是我正在使用的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>

这里生成的java类实现了

Cloneable, CopyTo, Equals, ToString interfaces
我尝试了
<arg>-Xserializable</arg>
但是它不起作用,我需要这个生成的类也应该实现
Serializable interface

java xml serialization xsd
3个回答
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>

绑定.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>

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

0
投票

对我有用的是像这样配置 Maven 插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
          ...
        </execution>
        <execution>
            <id>schema_variations-scopes</id>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                ...
                <xjbSources>
                    <xjbSource>src/main/resources/xjc/bindings.xjb</xjbSource>
                </xjbSources>
            </configuration>
        </execution>
    </executions>
</plugin>

并将绑定.xml文件放置在路径下:

src/main/resources/xjc/bindings.xjb

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

© www.soinside.com 2019 - 2024. All rights reserved.