簡體   English   中英

JAXB中不同xmls / roots中的子元素的共享類

[英]Shared class for child element in JAXB in different xmls/roots

JAXB中使用xsd方案中的xjc自動生成類時。

alpha.xsd

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="alpha">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="persons">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="person" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

beta.xml

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="country">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="class">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="person">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

正如您所看到的,在這兩個方案之間共享Person元素。 我想做的是:

  • 使用xjc生成類,以便為兩個模式類共享ObjectFactory類(輸出類將在一個包中)
  • 不使用嵌套的靜態類(具有屬性localScoping="toplevel"
  • 使用Person類與/alpha/persons/person綁定,因為/country/class/person沒有創建兩個Person類

這樣做的目的是解組一個xml,應用業務邏輯並創建另一個作為輸出,其中一些元素(如Person )相同並為兩個xml文件共享。 兩個文件的命名空間都是相同的。

如果您能向我提供完整的.xjb綁定設置文件,我將不勝感激。 到目前為止我的僅包含:

<jxb:bindings version="1.0" 
  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:extensionBindingPrefixes="xjc">

  <jxb:globalBindings localScoping="toplevel"/>
</jxb:bindings>

當然我得到名稱沖突錯誤,因為我不知道如何設置綁定編譯器以將Person視為相同的實體/元素。

您可以使用外部綁定文件來指示在類生成期間,我們希望將現有類用於名為Document的復雜類型。

binding.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="beta.xsd">
        <jxb:bindings node="//xs:element[@name='person']/complexType">
            <jxb:class ref="alpha.Person"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

XJC電話

xjc -b binding.xml beta.xsd

如果來自Aperson 名稱空間將等於來自B的 名稱空間 person ,則該xjc必須生成正確的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM