2017-09-05 1 views
1

私は次のような構成のxml表現を持っています。JAXBとインスタンス化するクラスの動的選択

<definitions> 
    <definition type="MessageReception"> ... </definition> 
    <definition type="MessageProcessing"> ... </definition> 
    <definition type="ResponseGeneration"> ... </definition> 
</definition> 

ご覧のとおり、定義の種類は属性「タイプ」によって異なります。 JAXBフレームワークを使用してアンマーシャリングしたいと思います。しかし、タイトル、著者、年などのフラットな属性を持つ本のような、非常に基本的なケースを持つJAXBの使用例を見つけるには...

私は何をしたいのですか?

答えて

0

"定義"の内部クラスを作成するときは、 "type"メンバに@XmlAttributeという注釈を付ける必要があります。

ここでは、与えられたxmlの基本的なデモンストレーションです。

public class UnmarshalJaxbDemo { 


    public static void main(String[] args) { 
     StringBuffer xmlStr = new StringBuffer("<definitions>"+ 
            "<definition type=\"MessageReception\"> ... </definition>"+ 
            "<definition type=\"MessageProcessing\"> ... </definition>"+ 
            "<definition type=\"ResponseGeneration\"> ... </definition>"+ 
            "</definitions>"); 
     try { 
      JAXBContext context = JAXBContext.newInstance(Definitions.class); 
      Unmarshaller jaxbUnmarshaller = context.createUnmarshaller(); 
      Definitions definitions = (Definitions) jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(xmlStr.toString()))); 

      for (Definition defitinion : definitions.getDefinition()) { 
       System.out.println(defitinion.getType()); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class Definition { 

     @XmlAttribute 
     private String type; 

     public String getType() { 
      return type; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

    } 

    @XmlRootElement(name = "definitions") 
    @XmlAccessorType(XmlAccessType.FIELD) 
    public static class Definitions { 
     private List<Definition> definition; 

     public List<Definition> getDefinition() { 
      return definition; 
     } 

     public void setDefinition(List<Definition> definition) { 
      this.definition = definition; 
     } 

    } 

} 
+0

こんにちは、お返事ありがとうございます。私の質問は具体的ではないので、私はそれを編集します: "タイプ"属性に応じて異なるサブタイプのインスタンスを生成したい、それは私の謎です。 – Joel

0

xsi:typeを使用して、インスタンス化するクラスのjaxbに指示できます。 例:

<definitions> 
    <definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="messageReception"> 
     <receptionField>foo</receptionField> 
    </definition> 
    <definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="messageProcessing"> 
     <processingField>bar</processingField> 
    </definition> 
    <definition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="responseGeneration"> 
     <generationField>baz</generationField> 
    </definition> 
</definitions> 

package your.package 

class MessageReception { 
    // getters and setters omitted 
    String receptionField; 
} 

jaxbContext = JAXBContext.newInstance("your.package"); 
Unmarshaller unmarshaller = mJaxbContext.createUnmarshaller(); 
DefinitionList definitionList = (DefinitionList) unmarshaller.unmarshal(inputStream); 
+0

ありがとう私はしようとしている! – Joel

関連する問題