如何在java

时间:2016-05-03 18:22:53

标签: java xsd jaxb xjc

我正在增强一个使用XSD和java xjc.exe编译器的桌面java应用程序来持久化和恢复对象数据。我需要保留包含double[][]要素数据的新类型。我补充说:

<xs:schema
    ...
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<xs:import namespace="http://schemas.xmlsoap.org/soap/encoding/"
    schemaLocation="http://schemas.xmlsoap.org/soap/encoding/"/>

<xs:complexType name="patchFeature">
    <xs:all>
        <xs:element name="featureMatrix" type="doubleMatrix" />
    </xs:all>
    <xs:attribute name="type" type="patchType" use="required" />
</xs:complexType>

<xs:complexType name="doubleMatrix">
<xs:complexContent>
 <xs:restriction base="soapenc:Array">
      <xs:attribute ref="soapenc:arrayType" 
                 wsdl:arrayType="xs:double[][]"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
...
</xs:schema>

XSD文件。 xjc.exe编译器可以编译它,但它生成的PatchFeatureDoubleMatrix类没有获取/设置double[][]值的方法。我如何坚持二维数组的原始双重?我不想使用XSD序列,因为它们生成的对象需要List<Double>,这需要装箱,取消装箱和转换为原始double[][]矩阵。

1 个答案:

答案 0 :(得分:0)

我终于放弃了尝试持久/恢复double [] []数组。相反,我坚持每行'stride'元素和一维ArrayList。我在double [] []数组和stride / ArrayList之间编写了转换器。它不是很优雅,但它有效。 xsl看起来像:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://novodynamics.com/NovoHealth/features.xsd"
    xmlns="http://novodynamics.com/NovoHealth/features.xsd"
    elementFormDefault="qualified">
    <xs:complexType name="patchFeature">
        <xs:all>
            <xs:element name="featureVector" type="doubleVector" />
        </xs:all>
        <xs:attribute name="stride" type="xs:int" use="required" />
    </xs:complexType>

    <xs:complexType name="doubleVector">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="dbl" type="xs:double" />
        </xs:sequence>
    </xs:complexType>
    ...

转换器:

private double[][] featureMatrix;

public void setFeatureMatrix(int stride, List<Double> vector) {
  this.featureMatrix = new double[vector.size() / stride][stride];
  for (int i = 0; i < vector.size(); i++) {
    this.featureMatrix[i / stride][i % stride] = vector.get(i);
  }
}

public static void createPatchFeature(double[][] featureMatrix, Feature feature,
    ObjectFactory factory) {
  PatchFeature patchFeature = factory.createPatchFeature();
  int stride = featureMatrix[0].length;
  // int vectorSize = stride * featureMatrix.length;
  patchFeature.setStride(stride);
  DoubleVector vector = factory.createDoubleVector();
  for (int i = 0; i < featureMatrix.length; i++) {
    for (int j = 0; j < stride; j++) {
      vector.getDbl().add(featureMatrix[i][j]);
    }
  }
  patchFeature.setFeatureVector(vector);
  feature.setPatch(patchFeature);
}