2016-04-16 11 views
2

WSDLファイルからJAXBクラスを生成しましたが、XMLをJavaオブジェクトに変換しようとしています。ここで生成されたJAXBクラスの例:XMLの生成されたクラスにxmlルート要素注釈を追加する方法は?

XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "GetProductListResponse", propOrder = { 
    "productList", 
    "productListDate", 
    "failed", 
    "failedDescription" 
}) 
public class GetProductListResponse { 

@XmlElementRef(name = "ProductList", namespace = "http://productService.productsdata", type = JAXBElement.class, required = false) 
protected JAXBElement<ArrayOfProductListDetail> productList; 
@XmlElementRef(name = "ProductListDate", namespace = "http://productService.productsdata", type = JAXBElement.class, required = false) 
protected JAXBElement<String> productListDate; 
@XmlElement(name = "Failed") 
protected boolean failed; 
@XmlElement(name = "FailedDescription", required = true, nillable = true) 
protected String failedDescription; 

... 
} 

例私はGetProductListResponseオブジェクトに変換する必要がありproducts.xmlファイル内に保存されており、それは次のようになります。

convertXmlProductsTest方法がある
<GetProductListResult xmlns="http://productService.productsdata"> 
      <ProductList> 
       <ProductListDetail> 
        <ProductName>SomeProductName</ProductName> 
        <ProductCost>9,45</ProductCost> 
       </ProductListDetail> 
       <ProductListDate>09.09.2015</ProductListDate> 
       <Failed>false</Failed> 
       <FailedDescription/> 
      </ProductList> 
</GetProductListResult> 

内部コンバージョンコールが設定されている場所:

public class ProductHandler { 

    public static GetProductListResponse convertXmlProductsTest(){ 
     try { 
      JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class); 
      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      GetProductListResponse retval = (GetProductListResponse) jaxbUnmarshaller.unmarshal(new File("products.xml")); 

      return retval; 
     } catch (JAXBException ex) { 
      Logger.getLogger(ProductMockWs.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     throw new UnsupportedOperationException("XML to Java object conversion failed."); 
    } 
} 

問題は生成されたJAXBクラスGetProductListResponseには@XmlRootElement注釈が含まれていないため、この変換は有名なエラーメッセージjavax.xml.bind.UnmarshalException: unexpected element ... Expected elements are ...で失敗します。変換が成功した

@XmlRootElement(name="GetProductsListResult") 
public class GetProductListResponse { ...} 

:私は手動でGetProductListResponseクラスに@XmlRootElement注釈を追加し、としてそれを設定

QUESTION: は、そのクラスの外から生成されたクラス(GetProductListResponse)について@XmlRootElementを設定する方法はありますか?

生成されたクラスのカスタマイズを避けたいと思い、WSDL定義を変更したくありません。また、ランタイムアノテーションの設定についても読んでいますが、JavassistなどのJavaバイトコードマニピュレータを使用する必要はありません。

答えて

3
JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class); 
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 

    JAXBElement<GetProductListResponse> root = jaxbUnmarshaller.unmarshal(new StreamSource(
      file), GetProductListResponse.class); 
    GetProductListResponse productListResponse = root.getValue(); 

@XmlRootElementが不足している場合、この記事は私を大きく助けました。見てみましょう:

http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

あなたがここにあなたのエラーを見つけ、あなたが何ができるかを見ることができます!それが役に立てば幸い !

0

Jackson json/xml parser libraryで可能です。 Jackson fully supports JAXB annotationsおよびそのmixin featureを使用すると、クラスの注釈をソースから外部に追加できます。

+0

お返事ありがとう@sharonbn!私はあなたが示唆したようにJacksonのライブラリを試しましたが、生成されたクラスの中にはセッターやアーギュメントのないコンストラクタ(つまりArrayOfProductListDetail)が生成されていないため、XMLを解析するのに問題があります。私はまた、mavenでjaxb annotateプラグインを使用し、バインディングjxbファイルを追加しようとしましたが、xsdファイルがひどく書かれているため、maven-jaxb2コンパイラは報告された衝突エラーを含むxsdファイル内の数百箇所。他に提案はありますか?前もって感謝します。 – hideburn

+0

申し訳ありませんが、他の提案はありません。 –

関連する問題