【问题标题】:How to generate Java files from WSDL when 'lang' already defined?已定义“语言”时如何从 WSDL 生成 Java 文件?
【发布时间】:2016-08-01 16:41:20
【问题描述】:

我正在尝试使用wsimport 通过运行以下命令将 WSDL 文件转换为 Java 代码(应该可以在任何人的机器上运行):

wsimport https://webservices-uatprod.dhisco.com/OTAHotelDescriptiveInfo/web_services?WSDL -J-Djavax.xml.accessExternalDTD=all -J-Djavax.xml.accessExternalSchema=all -B-XautoNameResolution -Xnocompile

但是,我不断收到此错误:

[ERROR] 'lang' is already defined
  line 93 of http://www.w3.org/2001/03/xml.xsd

[ERROR] (related to above error) the first definition appears here
  line 43 of http://www.w3.org/2001/xml.xsd

[ERROR] 'space' is already defined
  line 102 of http://www.w3.org/2001/03/xml.xsd

[ERROR] (related to above error) the first definition appears here
  line 89 of http://www.w3.org/2001/xml.xsd

[ERROR] 'base' is already defined
  line 109 of http://www.w3.org/2001/03/xml.xsd

[ERROR] (related to above error) the first definition appears here
  line 113 of http://www.w3.org/2001/xml.xsd

[ERROR] 'specialAttrs' is already defined
  line 117 of http://www.w3.org/2001/03/xml.xsd

[ERROR] (related to above error) the first definition appears here
  line 157 of http://www.w3.org/2001/xml.xsd

我花了几个小时在谷歌上搜索这个以尝试找到解决方案。我比较确信我需要用-b binding.xml 标志指定一个绑定文件。

但是,我很难弄清楚如何创建该绑定文件。这是我尝试过的:

binding.xml

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsd="http://www.w3c.org/2001/XMLSchema"
              xmlns:xs="http://www.w3.org/2001/03/xml.xsd"
              jaxb:version="2.0">
  <jaxb:bindings schemaLocation="http://www.w3.org/2001/xml.xsd">
    <jaxb:bindings node="//xs:attribute[@name='lang']">
        <jaxb:property name="langAttribute"/>
    </jaxb:bindings>
  </jaxb:bindings>
</jaxb:bindings>

然后,有了这个,我尝试通过以下方式运行绑定文件:

wsimport https://webservices-uatprod.dhisco.com/OTAHotelDescriptiveInfo/web_services?WSDL -J-Djavax.xml.accessExternalDTD=all -J-Djavax.xml.accessExternalSchema=all -B-XautoNameResolution -Xnocompile -b binding.xml

现在我得到了:

[ERROR] XPath evaluation of "//xs:attribute[@name='lang']" results in empty target node
  line 6 of file:/Users/name/git/foo/bar/src/main/resources/wsdl/binding.xml

我已经尝试了绑定文件的 XPath 的许多其他组合...我想我需要将所有元素的属性从“lang”重命名为其他名称,但我真的很难弄清楚。

提前感谢您的帮助!

解决方案更新: 我通过在本地下载架构克服了这个错误,并且无论在哪里引用 schemaLocation="http://www.w3.org/2001/03/xml.xsd"schemaLocation="http://www.w3.org/2001/xml.xsd",我都编辑了 XML 以指向文件系统上文件的本地副本。

即打开引用这些文件的每个 *.xsd 文件,并从以下内容更新每一行:

<xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>

到这里:

<xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="./xml.xsd"/>

之后,它能够使用上面的wsimport 语法生成 Java 类(确实需要一个小的绑定文件,但这与供应商定义的类有关)。

【问题讨论】:

    标签: java xpath jaxb xjc wsimport


    【解决方案1】:

    您已将 xs 前缀绑定到 XML 命名空间 http://www.w3.org/2001/03/xml.xsd,但它应该绑定到 XML Schema 命名空间 http://www.w3.org/2001/XMLSchema

    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    

    您将面临的另一个问题是您的架构似乎通过两个不同的位置寻址xml.xsdhttp://www.w3.org/2001/xml.xsdhttp://www.w3.org/2001/03/xml.xsd。这会给你带来很多重复。您可以尝试通过以下目录解决它们:

    REWRITE_SYSTEM "http://www.w3.org/2001/03/" "http://www.w3.org/2001/"
    

    (和-catalog一起使用。)

    但我不确定这是否可行。在类似的情况下,我创建了架构的完整本地副本,我需要对其进行编译和修补以使用统一的架构位置。

    我尝试在本地下载架构和 xml 文件,但不知道如何告诉 wsimport 查找本地副本而不是上网。如果我在本地有 xsd.xml 的副本......有没有办法告诉 wsimport 使用它而不是它在互联网上可能找到的任何东西?

    我不太确定wsimport,但通常它是通过目录完成的。假设您已经从目录w3c 中的http://www.w3.org 下载了模式。然后你会有一个像

    这样的目录文件
    REWRITE_SYSTEM "http://www.w3.org/" "w3c/"
    

    那么您应该可以通过wsimport -catalog mycatalog.cat ... 使用此目录文件。然后wsimport 或底层架构编译器xjc 应该从w3c/2001/xml.xsd 获取您的http://www.w3.org/2001/xml.xsd 架构。

    但是,我从未尝试过与wsimport 一起使用,我只是经常与maven-jaxb2-plugin 一起使用它。

    【讨论】:

    • 我尝试在本地下载模式和 xml 文件,但不确定如何告诉 wsimport 查找本地副本而不是上网。如果我在本地有xsd.xml 的副本...有没有办法告诉 wsimport 使用它而不是它在互联网上可能找到的任何东西?
    • 你用目录文件来做。我会尽快更新答案。
    • 我很确定maven-jaxb2 插件和wsimport 都在调用xjc 来完成很多工作。
    • 感谢您的帮助。我能够通过在本地下载所有模式并更新模式以指向“w3.org/2001/03/xml.xsd”“w3.org/2001/xml.xsd”的一个本地副本来编译它。 '当然,现在我正在处理下一个错误,我很确定这是特定于供应商的,但至少这个问题得到了解决。再次感谢。
    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2012-07-31
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多