2010-03-09 33 views
18

Tôi đang sử dụng jaxb để tạo các lớp java ra khỏi lược đồ xml. Giản đồ nhập khẩu XMLSchema.xsd và nội dung của nó được sử dụng như một phần tử trong tài liệu.Tạo các lớp Java ra khỏi XMLSchema.xsd bằng cách sử dụng JAXB

Nếu tôi xóa lần nhập và tham chiếu đến "xsd: schema" tương ứng thì trình biên dịch liên kết sẽ tạo thành công các lớp. Nếu tôi không làm như vậy, nó sẽ tạo ra các lỗi sau, cũng giống nhau nếu tôi cố gắng tạo các lớp Java chỉ từ XMLSchema.xsd!

> C:\Users\me>"%JAXB%/xjc" -extension -d tmp/uisocketdesc -p uis.jaxb uisocketdesc.xsd -b xml_binding_test.xml -b xml_binding_test_2.xml 
-b xml_binding_test_3.xml 
parsing a schema... 
compiling a schema... 

> [ERROR] A class/interface with the same name "uis.jaxb.ComplexType" is already in use. Use a class customization to resolve this conflict. 
line 612 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "ComplexType" is generated from here. 
line 440 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.Attribute" is already in use. Use a class customization to resolve this conflict. 
line 364 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "Attribute" is generated from here. 
line 1020 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.SimpleType" is already in use. Use a class customization to resolve this conflict. 
line 2278 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "SimpleType" is generated from here. 
line 2222 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.Group" is already in use. Use a class customization to resolve this conflict. 
line 930 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "Group" is generated from here. 
line 727 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.AttributeGroup" is already in use. Use a class customization to resolve this conflict. 
line 1062 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "AttributeGroup" is generated from here. 
line 1026 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] A class/interface with the same name "uis.jaxb.Element" is already in use. Use a class customization to resolve this conflict. 
line 721 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Relevant to above error) another "Element" is generated from here. 
line 647 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 1020 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 364 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 2278 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 2222 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 930 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 727 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 440 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 612 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 1026 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 1062 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] Two declarations cause a collision in the ObjectFactory class. 
line 647 of "http://www.w3.org/2001/XMLSchema.xsd" 

> [ERROR] (Related to above error) This is the other declaration. 
line 721 of "http://www.w3.org/2001/XMLSchema.xsd" 

Failed to produce code. 

Trả lời

4

Dường như giản đồ của bạn bị hỏng. XJC là cực kỳ cầu kỳ về những điều này và có thể thất bại về những thứ mà các công cụ khác cho phép.

Thay vì chọn thông qua lỗi XJC, tôi thấy dễ dàng hơn khi chạy sơ đồ thông qua số AlphaWorks Schema Quality Checker trước tiên. Điều này mang lại kết quả tốt, có thể đọc được (tốt, có thể phát triển được, dù sao) của những gì là sai. Nếu nó thông qua OK, bạn sẽ có cơ hội tốt hơn để vượt qua XJC.

+1

Cảm ơn mẹo về SQC mặc dù nó không nhận ra những vấn đề nêu trên. – david

+0

Liên kết dường như bị hỏng. Bạn có thể vui lòng cung cấp cái mới không? –

+0

Tôi đã sử dụng https://www.wsdl-analyzer.com –

-1

Bạn có thể sử dụng xjc đi kèm với JDK1.6. Ngoài ra, bạn có thể thử XML với Java, tham khảo this article.

+0

OP đã sử dụng xjc –

9

Bạn có thể nhận JAXB thành công để tạo mã cho XSD Lược đồ XML bằng (ít nhất) hai phương thức. Vấn đề bạn đang gặp phải xuất phát từ thực tế là một số loại lược đồ và phần tử chia sẻ cùng tên.


Tùy chọn đầu tiên tránh xung đột tên bằng cách áp dụng một hoặc nhiều biến đổi tên XML cho tên lớp đã tạo. Dưới đây là một ví dụ về một tập tin ràng buộc bên ngoài mà sẽ làm điều này:

<jxb:bindings version="2.1" 
       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> 
    <xjc:simple/> 
    </jxb:globalBindings> 
    <jxb:bindings schemaLocation="XMLSchema.xsd"> 
    <jxb:schemaBindings> 
     <jxb:nameXmlTransform> 
     <jxb:elementName suffix="Element"/> 
     </jxb:nameXmlTransform> 
    </jxb:schemaBindings> 
    </jxb:bindings> 
</jxb:bindings> 

cách tiếp cận này hoạt động (bạn có thể áp dụng biến đổi khác mà sẽ chỉ làm việc tốt, nhưng tôi đã nhìn thấy kỹ thuật này hơn và hơn trong tài liệu) nhưng tôi nghĩ nó tạo ra kết quả xấu xí. Ví dụ: có một lớp được tạo trong trường hợp này có tên là ElementElement.


Cách tiếp cận thứ hai sử dụng tùy chỉnh lớp như được đề xuất bởi đầu ra của xjc. Trong thực tế, tất cả nhưng một trong các lớp vấn đề có thuộc tính abstract="true" được đặt trong loại lược đồ tương ứng. Do đó, tôi có ý nghĩa với việc thêm tên lớp với "Tóm tắt", tức là AbstractElement. Lớp Attribute còn lại không trừu tượng, nhưng xs:element có tên attribute tạo ra một lớp mở rộng với một phần trống. Do đó tôi gọi nó là BaseAttribute. Đây là định nghĩa ràng buộc bên ngoài mà tôi đã sử dụng:

<jxb:bindings version="2.1" 
       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> 
    <xjc:simple/> 
    </jxb:globalBindings> 
    <jxb:bindings schemaLocation="XMLSchema.xsd"> 
    <jxb:schemaBindings> 
     <jxb:package name="org.w3.xmlschema"/> 
    </jxb:schemaBindings> 
    <jxb:bindings node="//xs:complexType[@name='complexType']"> 
     <jxb:class name="AbstractComplexType"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='group']"> 
     <jxb:class name="AbstractGroup"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='attributeGroup']"> 
     <jxb:class name="AbstractAttributeGroup"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='simpleType']"> 
     <jxb:class name="AbstractSimpleType"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='element']"> 
     <jxb:class name="AbstractElement"/> 
    </jxb:bindings> 
    <jxb:bindings node="//xs:complexType[@name='attribute']"> 
     <jxb:class name="BaseAttribute"/> 
    </jxb:bindings> 
    </jxb:bindings> 
</jxb:bindings> 

Điều này cung cấp tên lớp được tạo rõ ràng hơn theo ý kiến ​​của tôi và biên soạn thành công lược đồ.


Tôi cũng muốn khuyên bạn sử dụng biên soạn riêng cho XMLSchema.xsd và giữ nó trong một file jar để sử dụng sau trong trường hợp vấn đề này đã đưa ra một lần nữa trong khi soạn thảo sơ đồ khác. Câu trả lời cho this question mô tả cách thức.

+0

đối mặt với sự cố tương tự. Nhưng tôi có khoảng 50 biến như vậy bất kỳ giải pháp thay thế? – wib

14

Đấu tranh với những thứ như vậy, đối với tôi đó là vấn đề về độ nhạy và có cùng tên cho vấn đề phần tử và thuộc tính (đôi khi thừa kế đôi khi).Đối với vấn đề thứ hai tôi đang sử dụng tập tin ràng buộc bên ngoài có chứa một cái gì đó như thế này:

<jaxb:bindings node="//xs:complexType[@name='AbstractGriddedSurfaceType']//xs:attribute[@name='rows']"> 
    <jaxb:property name="rowCount"/> 
</jaxb:bindings> 

Đối với vấn đề trường hợp nhạy cảm, bạn có thể sử dụng XJC luận -XautoNameResolution, phiên bản maven là <args><arg>-B-XautoNameResolution</arg></args>, nhưng điều này không làm việc đối với các phần tử bao bọc, vì vậy bạn cần phải viết các tùy chỉnh jaxb như đã đề cập ở trên, ... và nếu bạn không may mắn như tôi :), bạn có thể cần sử dụng bản sao cục bộ của xsd và sửa các bản sao theo cách thủ công.

Sửa Tìm thấy một giải pháp cho vấn đề trường hợp nhạy cảm nơi đổi tên tên nguyên tố là không đủ:

<jaxb:bindings node=".//xs:element[@name='imageDatum'][@type='gml:ImageDatumPropertyType']"> 
    <jaxb:property name="imageDatumInst"/> 
    <jaxb:factoryMethod name="imageDatumInst" /> 
</jaxb:bindings> 

Chúc may mắn, hy vọng điều này sẽ giúp.

+7

-XautoNameResolution đã giải quyết nó cho tôi :-D –

2

Tôi gặp phải sự cố tương tự khi tạo đối tượng từ .wsdl bằng cxf. Độ phân giải cho tôi là sử dụng -autoNameResolution như thông báo lỗi gợi ý. cấu hình Maven:

  <wsdlOption> 
          <wsdl>${basedir}/test.wsdl</wsdl> 
          <extraargs> 
           <extraarg>-b</extraarg>  
          <extraarg>http://www.w3.org/2001/XMLSchema.xsd</extraarg> 
           <extraarg>-autoNameResolution</extraarg> 
          </extraargs> 
          <packagenames> 
           <packagename>test.wsdl</packagename> 
          </packagenames> 
         </wsdlOption> 
0

tôi đã cố gắng sau đây và nó làm việc cho tôi:

<jxb:bindings schemaLocation="ClaimActivity-ACORD.xsd"> 
    <jxb:schemaBindings> 
     <jxb:package name="x.x.x.x" /> 
    </jxb:schemaBindings> 
    <jxb:bindings node="//xsd:complexType[@name='ConstructionType']"> 
     <jxb:class name="AbstractConstructionType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='ItemDefinitionType']"> 
     <jxb:class name="AbstractItemDefinitionType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='LocationType']"> 
     <jxb:class name="AbstractLocationType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='TaxFeeType']"> 
     <jxb:class name="AbstractTaxFeeType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='DeductibleType']"> 
     <jxb:class name="AbstractDeductibleType" /> 
    </jxb:bindings> 
    <jxb:bindings node="//xsd:complexType[@name='RegistrationType']"> 
     <jxb:class name="AbstractRegistrationType" /> 
    </jxb:bindings> 
</jxb:bindings> 

1

Một lựa chọn khác sẽ được loại bỏ tùy chọn -p để các lớp được tạo ra trong các gói khác nhau

Các vấn đề liên quan