2017-09-05 3 views
1

다음과 같이 구성의 XML 표현이 있습니다.인스턴스화 할 JAXB 및 동적 선택

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

여기서 알 수 있듯이 정의 유형은 속성 "유형"에 따라 다릅니다. JAXB 프레임 워크를 사용하여 이것을 언 마샬하고 싶습니다. 그러나 제목, 저자, 연도와 같은 평면 속성이있는 책과 같은 매우 기본적인 경우에만 JAXB 사용 예제를 찾습니다.

내가 원하는 것을 쉽게 할 수있는 방법이 있습니까?

답변

0

"정의"에 대한 내부 클래스를 만들 때 @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