阅读xmlAttribute从XmlElement的在JAXB

问题描述:

你好我有以下XML:
阅读xmlAttribute从XmlElement的在JAXB

用下面的代码:

我怎样才能得到每个元素的属性DispalyName没有唐氏创建新类和Up并使用@xmlAttribute。 我可以通过添加新的2个类UpElement和DownElement来解决它,并为每个类获取@xmlAttribute,但我想在一次类。

Code Example: 

     import javax.xml.bind.annotation.XmlAccessorType; 
     import javax.xml.bind.annotation.XmlElement; 
     import javax.xml.bind.annotation.XmlRootElement; 


     @XmlRootElement(name = "StatusesList") 
     @XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD) 
     public class StatusesListElement 
     { 

      @XmlElement(name = "Down") 
      private String down = ""; 


      @XmlElement(name = "Up") 
      private String up = ""; 


      public String getDown() 
      { 
       return down; 
      } 


      public void setDown(String down) 
      { 
       this.down = down; 
      } 


      public String getUp() 
      { 
       return up; 
      } 


      public void setUp(String up) 
      { 
       this.up = up; 
      } 


     } 

注:我是EclipseLink JAXB (MOXy)铅和JAXB 2 (JSR-222)专家小组的成员。

使用的EclipseLink JAXB(莫西)

您可以使用@XmlPath扩展在莫西映射这个用例:

import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 


@XmlRootElement(name = "StatusesList") 
@XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD) 
public class StatusesListElement 
{ 

    @XmlPath("Down/@DisplayName") 
    private String down = ""; 

    @XmlElement(name = "Up/@DisplayName") 
    private String up = ""; 

} 

使用任何JAXB实现

你这个用例可以使用XmlAdapter。下面是一个答案我给了一个链接演示了如何可以做到这一点:

+0

非常感谢您的快速回答 – user1205079 2012-03-27 15:55:45

+0

是否有设置在运行时@XmlElement的选项?而不是硬编码 – user1205079 2012-03-27 18:35:09