【问题标题】:jaxb anyelement namespace any instead of otherjaxb anyelement 命名空间 any 而不是 other
【发布时间】:2017-07-22 09:01:12
【问题描述】:

我有一个 java jaxb 注释类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement()
public class xmlDoc<T> {
@XmlMixed
@XmlAnyElement(lax=false)
   protected T content;
   public T getContent() {
      return this.content;
   }
   public void setContent(T t) {
        this.content = t;
   }
}

当我使用 jaxb 生成 xml 架构时,我得到以下输出

<xs:complexType mixed="true" name="xmlDoc">
    <xs:sequence>
      <xs:any namespace="##other" processContents="skip"/>
    </xs:sequence>
  </xs:complexType>

jaxb 中是否有任何注释参数,我可以使用它来控制任何元素类型的命名空间。我需要##any 而不是##other。 这可能吗?

【问题讨论】:

    标签: java jaxb


    【解决方案1】:

    没有参数来控制细节的输出。但是,当生成 wsdl 时,如果我将命名空间的参数修改为 ##any,它将允许转换为现有数据类型并根据需要工作。

    【讨论】:

    • 这是否意味着,您要在每一代之后手动更改生成的 xsd 文件?这不是问题的真正解决方案,对吧?您是否还使用JAXBContext.generateSchema() 来生成文件?我需要依赖模式生成器输出##any。如果你真的能解决它,请分享你做了什么,或者只是再次打开问题让其他人回答。
    • 好的,我查看了参考实现的源代码,发现XmlSchemaGenerator 只是为任何元素编写了##other 硬编码。所以我们能做的不多。我将尝试编写一个小程序,将 ##other 替换为 ##any,这样我就不必在每一代都手动执行此操作。
    【解决方案2】:

    正如我在上面的评论中提到的,我查看了参考实现的源代码,发现XmlSchemaGenerator 只是为 any##other 硬编码/em> 元素。

    所以,我现在用以下方法在文件生成后将 ##other 替换为 ##any

      private static void fixNamespaceOfAnyElementsFor(final File xsdFile) throws IOException, FileNotFoundException {
        final File tempFile = File.createTempFile("your_prefix", ".tmp");
        Files.move(xsdFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        try (final BufferedWriter writer = Files.newBufferedWriter(xsdFile.toPath())) {
          try (final Stream<String> lines = Files.lines(tempFile.toPath())) {
            lines.map(line -> line.replace("namespace=\"##other\"", "namespace=\"##any\""))
                .forEach(line -> {
                  try {
                    writer.write(line);
                    writer.newLine();
                  } catch (final IOException e) {
                    throw new UncheckedIOException(e);
                  }
                });
          }
        }
        Files.delete(tempFile.toPath());
      }
    

    它基本上将文件移动到一个临时文件夹,以便我可以在原始位置重写它以替换属性值。完成后临时文件被删除。

    注意:首先我探索了 DOM 和 Stax 的解决方案,但它们都弄乱了生成器的原始格式(漂亮的打印)。由于我将生成的 XSD 提交给 git,并且每一代都需要一致的格式,因此我决定采用简单的逐行解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-07
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多