4

I'm trying to parse a list of BART stations that looks like this: https://api.bart.gov/docs/stn/stns.aspx using Simple XML (link: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php). I have set up my deserialized object as such:

StationList object:

@Root(name = "root")
public class StationList {
    @ElementList(name = "stations", inline = true)
    private List<Station> stations;

    @Element(name = "message", required = false)
    private String message;

    public StationList() {
    }

    public List<Station> getStations() {
        return stations;
    }

    public String getMessage() {
        return message;
    }
}

Station object:

@Root(name = "station", strict = false)
public class Station {
    @Element(name ="name")
    private String name;

    @Element(name = "abbr")
    private String abbr;

    @Element(name = "gtfs_latitude")
    private double latitude;

    @Element(name = "gtfs_longitude")
    private double longitude;

    @Element(name = "address")
    private String address;

    @Element(name = "city")
    private String city;

    @Element(name = "county")
    private String county;

    @Element(name = "state")
    private String state;

    @Element(name = "zipcode")
    private int zipCode;

    public String getName() {
        return name;
    }

    public String getAbbr() {
        return abbr;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public String getAddress() {
        return address;
    }

    public String getCounty() {
        return county;
    }

    public String getState() {
        return state;
    }

    public int getZipCode() {
        return zipCode;
    }
}

Regardless of what I try, I am never able to successfully parse a list of Stations. What am I doing wrong?

1 Answer 1

5

If you want to convert this xml file :

<?xml version="1.0" encoding="utf-8" ?> 
<root>
<uri><![CDATA[ http://api.bart.gov/api/stn.aspx?cmd=stns ]]></uri>
  <stations>
    <station>
      <name>12th St. Oakland City Center</name> 
      <abbr>12TH</abbr>
      <gtfs_latitude>37.803664</gtfs_latitude>
      <gtfs_longitude>-122.271604</gtfs_longitude>
      <address>1245 Broadway</address> 
      <city>Oakland</city> 
      <county>alameda</county> 
      <state>CA</state> 
      <zipcode>94612</zipcode> 
    </station>
    ...
    <station>
      <name>West Oakland</name> 
      <abbr>WOAK</abbr>
      <gtfs_latitude>37.80467476</gtfs_latitude>
      <gtfs_longitude>-122.2945822</gtfs_longitude>
      <address>1451 7th Street</address> 
      <city>Oakland</city> 
      <county>alameda</county> 
      <state>CA</state> 
      <zipcode>94607</zipcode> 
    </station>
  </stations>
  <message /> 
</root>

You have provided correct java classes but with few modifications it began to work. Look at my StationList class:

import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

@Root(name = "root")
public class StationList {
    @ElementList(name = "stations")
    private List<Station> stations;

    @Element(name = "message", required = false)
    private String message;
    @Element(name = "uri")
    private String uri;

    public StationList() {
    }

    public List<Station> getStations() {
        return stations;
    }

    public String getMessage() {
        return message;
    }

    public String getUri() {
        return uri;
    }

    public static void main(String[] args) throws Exception {
        Serializer serializer = new Persister();

        StationList example = serializer.read(StationList.class,
                StationList.class.getResourceAsStream("simplexml.xml"));
        System.out.println(example.getStations().size());
    }
}

It adds @Element String uri for <uri> xml tag and removes inline = true for <stations> tag

Sign up to request clarification or add additional context in comments.

2 Comments

It's still not working; I'm getting this exception: java.lang.RuntimeException: org.simpleframework.xml.core.ElementException: Element 'error' does not have a match in class com.example.jonathan.willimissbart.API.Models.StationsRoot at line 1
It's working perfectly. Finally a good example of a simplexml serializer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.