将XML属性解组为对象值

时间:2017-03-07 11:42:21

标签: java spring jaxb unmarshalling

我正在开展一个学校项目,我必须将一些XML值从API绑定到java对象。我能够获得所有元素,但我无法获得特定元素的属性。我四处寻找解决方案,却找不到解决方案。

我有这段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<>();
}

这就是我的unmarshaller看起来的样子。

// 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 个答案:

答案 0 :(得分:3)

添加&#34;更改&#34; DepartureTrack JAXB Generated类下的属性如下:

import javax.xml.bind.annotation.*;

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

    @XmlAttribute
    protected String changes;

 @XmlValue;
protected String content;

}

答案 1 :(得分: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; }


    }
}

并且很少有站点可以从xml或json创建java类。