JAXB解组具有相同名称的多个节点

时间:2015-05-07 22:30:13

标签: java arrays list jaxb

我有一个XML元素,其中包含我需要解组的同名节点。我需要List或Array中的服务。这是我的XML:

<provider name="foo">
    <service active="true" name="alias" timeout="N/A">value1</service>
    <service active="true" name="caption" timeout="N/A">value2</service>
    <service active="true" name="expect_manifest_file" timeout="15m">value3</service>
    <service active="true" name="expect_SD_and_HD_ADI" timeout="15m">value4</service>
</provider>

我只知道如何获取XML中的最后一个元素。如何列出所有<service>个节点,列表中是否包含activename属性?这是我的目标:

@XmlRootElement (name="provider")
public class Customer {
    String service;

    public String getService() {
        return service;
    }
    @XmlElement(name="service")
    public void setService(String service) {
        this.service = service;
    }
}

1 个答案:

答案 0 :(得分:0)

我会根据你的需要采取另一种方法,也许这就是你想要的,让我知道。

首先,我将创建Service类,它将处理Service节点的信息:

#main-page-content {
   max-width:960px;
   margin:auto;
   max-height: 500px;
   padding-top:25px;
   height:100%;
}

#balance {
    font-family:Lato;
    background-color:#00253F;
    color:#FFFFFF;
    font-size:24px;
    padding-left:15px;
    font-weight:bold;
    padding-right:10px;
    padding-top:15px;
    height:130px;
    width:52%;
    float:left;
}

#transact {
    font-family:Lato;
    background-color:#00253F;
    color:#FFFFFF;
    font-size:24px;
    padding-left:15px;
    font-weight:bold;
    padding-right:10px;
    padding-top:15px;
    height:130px;
    width:52%;
    float:left;
    margin-top:10px;
}

#history {
   background-color:#00253F;
   width:32%;
   margin-left: 58%;
   height:300px; 
}

.total-balance {
    padding-top: 8px;
    font-family:Lato;
    width:100%;
    float: left;
    font-size:14px; 
    height:16px;
}

.a-balance {
    padding-top: 8px;
    font-family:Lato;
    width:100%;
    float: left;
    font-size:14px; 
    height:16px;
}

.b-balance {
    padding-top: 8px;
    padding-bottom:8px;
    font-family:Lato;
    width:100%;
    float: left;
    font-size:14px; 
    height:16px;
}

.main-page-content-2 {
   width:100%;    
}

.main-page-content-2 > div {
    display:inline-block;
    vertical-align:top;
    color:#FFF;
}

.main-page-content-2 > div:first-child > div {
    margin:2px;
}


#history-inline {
   color:#FFF;
   background-color:#00253F;
   width:32%;
   margin-left: 58%;
   height:300px; 
}

然后我将创建“Customer”类,这是保存XML所包含的服务所需的:

@XmlRootElement(name = "service")
@XmlAccessorType (XmlAccessType.FIELD)

public class Service {

private boolean active;
private String name;

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

public String getName() {
    return name;
}

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

}

我在主类中准备了一些方法以确保它正常工作:

@XmlRootElement(name = "provider")
@XmlAccessorType (XmlAccessType.FIELD)

public class Customer {

@XmlElementWrapper
@XmlElement(name = "service")
private List<Service> services = null;

public List<Service> getServices() {
    return services;
}

public void setServices(List<Service> services) {
    this.services = services;
}

}

如果这对你有用,请告诉我。快乐的编码:)