[英]JAXB: Create XSD: two XMLs with same element names, but different content
[英]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
元素。 我想做的是:
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
如果來自A的person
名稱空間將等於來自B的 名稱空間 person
,則該xjc必須生成正確的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.