简体   繁体   English

将XML属性解编为对象值

[英]Unmarshal XML attribute to object value

I'm working on a school project where I have to bind some XML values from an API to a java object. 我正在一个学校项目上,我必须将一些XML值从API绑定到Java对象。 I am able to get all the elements, I can't however get the attribute of a specific element. 我能够获取所有元素,但是却无法获取特定元素的属性。 I've looked around for a solution, but couldn't find one. 我到处寻找解决方案,但是找不到。

I have this piece of XML code that I want to unmarshal with JAXB to a Java object. 我有这段XML代码,我想用JAXB解组到Java对象。 The attribute I would like to get is 'changes' in Departuretrack. 我想获得的属性是Departuretrack中的“ changes”。

<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>

I do currently have this object, it does work for all the elements. 我目前确实有这个对象,它确实适用于所有元素。 I don't know how to get the attribute 'changes' and put it into this object. 我不知道如何获取属性“ changes”并将其放入该对象。

@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;
}

I create a list with all the departures with this object. 我创建一个包含该对象所有离开的列表。

@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<>();
}

This is what my unmarshaller looks like. 这就是我的解组员的样子。

// 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;
}

Does anyone know how to get this attribute from the XML sheet and put it into the Java object? 有谁知道如何从XML表中获取此属性并将其放入Java对象?

Add the "changes" attribute under DepartureTrack JAXB Generated class as below: 在DepartureTrack JAXB Generated类下添加“ changes”属性,如下所示:

import javax.xml.bind.annotation.*;

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

    @XmlAttribute
    protected String changes;

 @XmlValue;
protected String content;

}

You should have java classes like below 您应该具有如下的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; }


    }
}

and there are few sites which provide to create java classes from the xml or json. 而且很少有网站可以从xml或json创建Java类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM