提问人:Jonathan Chiou 提问时间:7/4/2017 更新时间:7/4/2017 访问量:7168
Java:使用简单 XML 解析 XML
Java: Parsing XML With Simple XML
问:
我正在尝试解析如下所示的 BART 站列表:https://api.bart.gov/docs/stn/stns.aspx 使用简单 XML(链接:http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php)。我已经将我的反序列化对象设置为:
StationList 对象:
@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 对象:
@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;
}
}
无论我尝试什么,我都无法成功解析电台列表。我做错了什么?
答:
5赞
Jay Smith
7/4/2017
#1
如果要转换此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());
}
}
它添加 for xml 标签并删除 for 标签@Element String uri
<uri>
inline = true
<stations>
评论
0赞
Jonathan Chiou
7/4/2017
它仍然不起作用;我收到此异常:java.lang.RuntimeException:org.simpleframework.xml.core.ElementException:元素“error”在第 1 行的类 com.example.jonathan.willimissbart.API.Models.StationsRoot 中没有匹配项
0赞
yuriscom
11/3/2019
它工作得很好。最后,一个简单的 xml 序列化程序的好例子
评论