2016-06-10 2 views
0

@XmlElements을 사용할 때 같은 유형에 다른 이름을 지정할 수 있습니까? 처음에는 @XmlElement을 사용하기 시작했습니다. 약간의 독서를했고 @XmlElementWrapper@XmlElements을 찾았지만 여전히 원하는 출력을 얻을 수 없었습니다. 필자는 2에 대해 서로 다른 데이터 유형을 만들 수 있다는 것을 알았지 만 주석으로이를 수행 할 수 있다면 멋질 것입니다.동일한 유형이지만 이름이 다른 여러 요소가 있습니까?

현재 반복 :

@XmlRootElement(name = "Root") 
public class XmlTest { 

    @XmlElementWrapper(name="ContactInformation") 
    @XmlElements({ 
      @XmlElement(name="Name"), 
      @XmlElement(name="LogicalOwner") 
    }) 
    public List<String> contactInformation; 
    ... 
     contactInformation = new ArrayList<>(); 
     contactInformation.add("should be inside name"); 
     contactInformation.add("should be insde of owner"); 
    ... 

전류 출력 :

<Root> 
    <ContactInformation> 
     <LogicalOwner>should be inside name</LogicalOwner> 
     <LogicalOwner>should be insde of owner</LogicalOwner> 
    </ContactInformation> 
</Root> 

원하는 출력 :

<Root> 
    <ContactInformation> 
     <Name>should be inside name</Name> 
     <LogicalOwner>should be insde of owner</LogicalOwner> 
    </ContactInformation> 
</Root> 

답변

0

당신의 공동 uld는 필드를 저장하는 클래스를 만든 다음 해당 클래스를 직렬화합니다.

@XmlRootElemnt(name="root") 
Class ContactInformation{ 

    private String name; 

    @XmlElement(name="Name") 
    public String getName() { 
     return this.name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

저는 Gson.toJson()을 사용하는 데 익숙해 져서 XML 로의 직렬화에별로 도움이되지 않습니다. 잘하면이 올바른 방향으로 당신을 가리 킵니다.

+0

예 ... 나는 다른 수업을 만들지 않기를 바랬습니다. 그러나 이것은 효과가있을 것입니다. 이것은 한 수업에서 모든 것을 할 수있는 방법을 찾지 못하면 내 계획 B가 될 것입니다. – gonzo

관련 문제