Java:使用Simple XML解析XML

4
我将尝试解析一个类似于这样的BART车站列表: https://api.bart.gov/docs/stn/stns.aspx,使用Simple XML(链接:http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php)。我已经设置了如下的反序列化对象:

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

站点对象:
@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;
    }
}

无论我尝试什么,都无法成功解析站点列表。我做错了什么?
1个回答

5
如果您想将此XML文件转换为其他格式:
<?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>

您提供了正确的Java类,但经过一些修改后才开始工作。请看我的 StationList 类:

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

它为<uri>XML标记添加了@Element String uri,并删除了<stations>标记的inline=true


还是不行,我得到了这个异常:java.lang.RuntimeException: org.simpleframework.xml.core.ElementException: 元素“error”在类com.example.jonathan.willimissbart.API.Models.StationsRoot的第1行没有匹配项。 - Jonathan Chiou
它完美地运行着。终于有一个简单的XML序列化器的好例子了。 - yuriscom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接