Rodi
Rodi

Reputation: 122

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. 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. The attribute I would like to get is 'changes' in 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>

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.

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

Upvotes: 0

Views: 2621

Answers (2)

NIrav Modi
NIrav Modi

Reputation: 6992

You should have java classes like below

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.

Upvotes: 1

mhasan
mhasan

Reputation: 3709

Add the "changes" attribute under DepartureTrack JAXB Generated class as below:

import javax.xml.bind.annotation.*;

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

    @XmlAttribute
    protected String changes;

 @XmlValue;
protected String content;

}

Upvotes: 4

Related Questions