代码之家  ›  专栏  ›  技术社区  ›  nevets1219

使用XJC将XML模式转换为Java类

  •  9
  • nevets1219  · 技术社区  · 16 年前

    我使用xjc从XML模式生成Java类,下面是XSD的摘录。

    <xs:element name="NameInfo">
      <xs:complexType>
        <xs:sequence>
          <xs:choice>
            <xs:element ref="UnstructuredName"/> <!-- This line -->
            <xs:sequence>
              <xs:element ref="StructuredName"/>
              <xs:element ref="UnstructuredName" minOccurs="0"/> <!-- and this line! -->
            </xs:sequence>
          </xs:choice>
          <xs:element ref="SomethingElse" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    

    大多数情况下,生成的类都很好,但对于上面的块,我会得到如下结果:

    public List<Object> getContent() {
      if (content == null) {
        content = new ArrayList<Object>();
      }
      return this.content;
    }
    

    上面有以下评论:

    * You are getting this "catch-all" property because of the following reason: 
    * The field name "UnstructuredName" is used by two different parts of a schema. See: 
    * line XXXX of file:FILE.xsd
    * line XXXX of file:FILE.xsd
    * To get rid of this property, apply a property customization to one 
    * of both of the following declarations to change their names: 
    * Gets the value of the content property.
    

    我在这两行的末尾加了一句话。

    目前,我认为更改模式并不容易,因为这是由供应商决定的,我不想走这条路(如果可能的话),因为这会大大减慢进度。

    this page

    编辑:我应该澄清一下 UnstructuredName

    4 回复  |  直到 16 年前
        1
  •  1
  •   skaffman    16 年前

    <xs:sequence> 由一个 <xs:choice> List “所有的东西”。Java的类型结构不够灵活,无法更好地表示这一点。

    可以 帮助你,但在这种情况下,我怀疑没有,因为我找不到更好的方法来表示这些信息。

    我在过去使用过的另一种技术是,首先通过简单的XSLT转换传递模式,将组件重新安排为更加JAXB友好的组件,同时仍然允许文档具有与实际相同的结构。这样,您就可以“更改”模式而不必更改原始模式。

        2
  •  5
  •   Wojciech Owczarczyk    14 年前

    也可以使用名为 <xjc:simple /> :

    <?xml version="1.0" encoding="UTF-8"?>
    <jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
                  jxb:version="2.0">
            <jxb:globalBindings>
                <xjc:simple />
            </jxb:globalBindings>
    </jxb:bindings>
    

    更多信息 here

        3
  •  2
  •   Wonder    15 年前

    我也有同样的问题。我切换到xmlbeans和axis。XMLBeans可以编译您的模式,而不会出现问题和麻烦。JaxB无法处理这个问题。为了让JaxB处理这个问题,您可以稍微更改一下模式。

      <xs:sequence>
              <xs:choice>
        <!-- changed the following line -->
                <xs:element name="UnstructuredTop" type="UnstructuredName"/> 
        <!-- end of change -->
                <xs:sequence>
                  <xs:element ref="StructuredName"/>
                  <xs:element ref="UnstructuredName" minOccurs="0"/>
                </xs:sequence>
              </xs:choice>
              <xs:element ref="SomethingElse" minOccurs="0"/>
        </xs:sequence>
    

    然后JaxB将区分这两种情况,而不是翻转出来。

    但是你的处境和我的处境一样。改变模式是不可能的。所以我使用了xmlBeans和axis(即sux)。

        4
  •  0
  •   bajistaman    8 年前

    我创建了一个包装类来解决这个问题:

    List<JAXBElement<?>> contentList = address.getContent();
    if (contentList != null && contentList.size() > 0) {
        Address4JaxbMula address4JaxbMula = new Address4JaxbMula(contentList);
    

    }

    ...

    public static class Address4JaxbMula {
        public CountryCodeType countryCode;
        public AddressFixType addressFix;
        public String addressFree;
    
        public Address4JaxbMula(List<JAXBElement<?>> contentList) {
            if (contentList != null && contentList.size() > 0) {
                for (JAXBElement<?> content : contentList) {
                    Object value = content.getValue();
                    if (value.getClass().isAssignableFrom(CountryCodeType.class)) {
                        countryCode = (CountryCodeType) content.getValue();
                    } else if (value.getClass().isAssignableFrom(AddressFixType.class)) {
                        addressFix = (AddressFixType) content.getValue();
                    } else {
                        addressFree = (String) value;
                    }
                }
            }
        }
    
    }