2017-03-07 129 views
0

我正在研究一個學校項目,我必須將某些XML值從API綁定到Java對象。我能夠獲得所有元素,但我無法獲得特定元素的屬性。我四處尋找解決方案,但找不到一個解決方案。將XML屬性解組爲對象值

我有一段XML代碼,我想用JAXB將其解組爲Java對象。我想獲得的屬性是Departuretrack中的「變化」。

<Departures> 
    <DepartingTrain> 
     <Id>220</Id> 
     <DepartureTime>2017-03-07T11:03:00+0100</DepartureTime> 
     <DepartureTrack changes="false">5</DepartureTrack> 
    </DepartingTrain> 
    <DepartingTrain> 
     <Id>637</Id> 
     <DepartureTime>2017-03-07T11:18:00+0100</DepartureTime> 
     <DepartureTrack changes="false">12</DepartureTrack> 
    </DepartingTrain> 
</Departures> 

我目前有這個對象,它確實適用於所有的元素。我不知道如何獲得屬性「更改」並將其放入此對象。

@Entity 
@Getter 
@Setter 
@NoArgsConstructor 
@XmlRootElement(name="Departures") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Departure { 
    @Id 
    @GeneratedValue 
    private long id; 
    @XmlElement(name="Id") 
    private int routeNumber; 
    @XmlElement(name="DepartureTime") 
    private String departureTime; 
    @XmlElement(name="DepartureTrack") 
    private String departureTrack; 
} 

我創建了所有與此對象的離職名單。

@Entity 
@Getter 
@Setter 
@NoArgsConstructor 
@XmlRootElement(name="Departures") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class DepartureList { 

    @Id 
    @GeneratedValue 
    private long id; 
    @XmlElement(name="DepartingTrain") 
    @OneToMany 
    private List<Departure> departures = new ArrayList<>(); 
} 

這就是我的反編碼器的樣子。

// Returns all departures for a specific station 
public DepartureList getDepartingTrains(String station){ 
    try { 
     URL url = new URL("API URL" + station); 
     URLConnection urlConnection = url.openConnection(); 
     urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); 
     InputStream is = urlConnection.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is); 

     Unmarshaller unmarshaller = departureListJaxbContext.createUnmarshaller(); 
     DepartureList departureList = (DepartureList) unmarshaller.unmarshal(isr); 
     return departureList; 
    } catch (JAXBException e) { 
     e.printStackTrace(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // Throw Exception 
    return null; 
} 

有誰知道如何從XML表中獲取該屬性並將其放入Java對象嗎?

回答

2

下添加如下DepartureTrack JAXB生成類的「修改」屬性:

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement 
public class DepartureTrack { 

    @XmlAttribute 
    protected String changes; 

@XmlValue; 
protected String content; 

} 
+0

正確的。如果屬性只包含布爾值文字(並且這些是必需的),您也可以將String更改爲布爾值(或布爾值)。 –

0

應該對Java類,如下面

import org.simpleframework.xml.Attribute; 
import org.simpleframework.xml.Element; 
import org.simpleframework.xml.ElementList; 
import org.simpleframework.xml.Root; 

import java.math.BigDecimal; 
import java.util.List; 

@Root(name = "Departures") 
public class Departures { 

    @ElementList(name = "DepartingTrain", inline = true, required = false) 
    List<DepartingTrain> departingTrain; 



    public List<DepartingTrain> getDepartingTrain() { return this.departingTrain; } 
    public void setDepartingTrain(List<DepartingTrain> _value) { this.departingTrain = _value; } 



    public static class DepartingTrain { 

     @Element(name="Id", required = false) 
     String id; 


     @Element(name="DepartureTime", required = false) 
     String departureTime; 


     @Element(name="DepartureTrack", required = false) 
     DepartureTrack departureTrack; 



     public String getId() { return this.id; } 
     public void setId(String _value) { this.id = _value; } 


     public String getDepartureTime() { return this.departureTime; } 
     public void setDepartureTime(String _value) { this.departureTime = _value; } 


     public DepartureTrack getDepartureTrack() { return this.departureTrack; } 
     public void setDepartureTrack(DepartureTrack _value) { this.departureTrack = _value; } 


    } 

    public static class DepartureTrack { 

     @Attribute(name="changes", required = false) 
     Boolean changes; 



     public Boolean getChanges() { return this.changes; } 
     public void setChanges(Boolean _value) { this.changes = _value; } 


    } 
} 

和存在提供創建Java類的幾個網站來自xml或json。

+0

問題是關於JAXB,而不是簡單的XML。 –

相關問題