gpt4 book ai didi

Java:用简单的 XML 解析 XML

转载 作者:行者123 更新时间:2023-12-04 02:04:07 24 4
gpt4 key购买 nike

我正在尝试解析如下所示的 BART 车站列表:https://api.bart.gov/docs/stn/stns.aspx使用简单 XML(链接:http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php)。我已经这样设置了我的反序列化对象:

站列表对象:

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

无论我尝试什么,我都无法成功解析 Stations 列表。我做错了什么?

最佳答案

如果你想转换这个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());
}
}

它添加了@Element String uri对于 <uri> xml 标记并删除 inline = true对于 <stations>标签

关于Java:用简单的 XML 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com