2016-04-14 18 views
1

私は2つのxsdファイルを持っています.1つは著者を宣言し、もう1つは他のxsdから本と輸入作家を宣言します。作者についてはJaxb multiple xml unmarshalling

<xs:schema targetNamespace="ro.utcluj.cs.model.author" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       elementFormDefault="qualified"> 

     <xs:element name="author"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="authorName" type="xs:string"/> 
        <xs:element name="authorAge" type="xs:integer"/> 
        <xs:element name="CNP" type="xs:string"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 

    </xs:schema> 

冊の場合は:

<xs:schema targetNamespace="ro.utcluj.cs.model.book" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:author="ro.utcluj.cs.model.author" 
      xmlns:tns="ro.utcluj.cs.model.book" 
      elementFormDefault="qualified"> 

    <xs:import namespace="ro.utcluj.cs.model.author" schemaLocation="author.xsd"/> 

    <xs:element name="book"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="bookName" type="xs:string"/> 
       <xs:element name="title" type="xs:string"/> 
       <xs:element ref="author:author"/> 
       <xs:element name="genre" type="tns:bookGenre"/> 
       <xs:element name="quantity" type="xs:integer"/> 
       <xs:element name="price" type="xs:integer"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:simpleType name="bookGenre"> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="ACTION"/> 
      <xs:enumeration value="HISTORY"/> 
     </xs:restriction> 
    </xs:simpleType> 


</xs:schema> 

は、私はいくつかのxmlファイルを書き、JAXBでそれらを非整列化します。著者は1つのxmlに書かれていて、別のxmlに書かれていて、Jaxbでそれらを非マーシャルしてどういうわけかそれらをマージできますか?あるいは、書籍に著者に依存するという事実のために、両方とも指定された名前空間で同じxmlで書かれなければなりませんか?

答えて

1

私はあなたが何をしたいのかはわかりませんが、2つの* .xmlファイルを書き、アンマーシャルする場合は、これらのうちどれを "books.xsd"に対して検証する必要があり、 "authors .xsd "

次はアンマーシャリング/マーシャリング機能を使用する方法を知っている必要があります。

は(https://springframework.guru/you-should-use-jaxb-generated-classes-for-restful-web-services/ OR http://www.beingjavaguys.com/2013/04/create-spring-web-services-using-maven.htmlはあなたが読むことができない場合)あなたがたXSDからクラスを生成しているとしましょう。 (http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

オブジェクトをBook and Authorタイプから取得した後、リフレクトを使用してBookオブジェクト(Author型から見ることができます)のフィールド "author"をタイプAuthorの非マーシャルオブジェクトに設定できます。あなたのauthor.xmlから形成されています。

もう1つの解決方法は、book.xmlとauthor.xmlファイルをStringとして、手動でStringの要素を見つけて置き換える関数を書くことです。次に、 "著者を本に入れる"ことができます。単純に文字列を整列解除すると、作成者用の完全な情報(author.xmlから)の完全なオブジェクトを持つことになります。

私はあなたにいくつかの方向性の幸運を願っています!