我正在为火车时刻表应用程序构建一个项目,我正在使用一个返回XML文件的API来操作我正在使用名为SWXMLHash的库的数据。
我需要提取出发站的名称及其目的地和出发时间。
我创建了一个用于存储所有这些数据的类,但我需要改进它,因为我无法提取目的地和出发时间。
我能够遍历xml文件并获取出发站的名称。
这是我的班级:
class Station {
var name : String
var destinations:[(dest:String, times:[Int])]
init(withName name: String, destinations:[(dest:String, times:[Int])]){
self.name = name
self.destinations = destinations
}
}
以下是我提取电台名称的代码:
// This code loops over the XML file and retrieves the name of the departure stations
// create an empty array of Station class
var stationsTest = [Station]()
// retrieve the name of the departure stations and add them to and array of Station
for elem in xml["root"]["station"].all{
var stations = elem["name"].element!.text!
var stationPlaceHolder = Station(withName: stations, destinations: [(dest:"",times:[1])])
stationsTest.append(stationPlaceHolder)
}
我的问题是如何以适当的出发时间获取每个电台的目的地
我认为问题在于我实施课程的方式,我需要找到更好的解决方案。
以下是我正在使用的XML文件示例:
<?xml version="1.0" encoding="utf-8"?><root><uri><! [CDATA[http://api.bart.gov/api/etd.aspx? cmd=etd&orig=ALL&ramdom=1454366707766]]></uri><date>02/01/2016</date>
<time>02:44:52 PM PST</time>
<station>
<name>Lake Merritt</name>
<abbr>LAKE</abbr>
<etd>
<destination>Daly City</destination>
<abbreviation>DALY</abbreviation>
<estimate>
<minutes>3</minutes>
</estimate>
<estimate>
<minutes>10</minutes>
</estimate>
<estimate>
<minutes>17</minutes>
</estimate>
</etd>
<etd>
<destination>Dublin/Pleasanton</destination>
<estimate>
<minutes>7</minutes>
</estimate>
<estimate>
<minutes>22</minutes>
</estimate>
<estimate>
<minutes>37</minutes>
</estimate>
</etd>
<etd>
<destination>Fremont</destination>
<estimate>
<minutes>4</minutes>
</estimate>
<estimate>
<minutes>14</minutes>
</estimate>
<estimate>
<minutes>19</minutes>
</estimate>
</etd>
<etd>
<destination>Richmond</destination>
<estimate>
<minutes>5</minutes>
</estimate>
<estimate>
<minutes>19</minutes>
</estimate>
<estimate>
<minutes>34</minutes>
</estimate>
</etd>
</station>
<station>
<name>Fruitvale</name>
<etd>
<destination>Daly City</destination>
<estimate>
<minutes>6</minutes>
</estimate>
<estimate>
<minutes>12</minutes>
</estimate>
<estimate>
<minutes>22</minutes>
</estimate>
</etd>
<etd>
<destination>Dublin/Pleasanton</destination>
<estimate>
<minutes>10</minutes>
</estimate>
<estimate>
<minutes>25</minutes>
</estimate>
<estimate>
<minutes>40</minutes>
</estimate>
</etd>
答案 0 :(得分:1)
我在SWXMLHash游乐场中使用了以下代码,它可以在我的机器上运行:
public ProgressSpinner()
{
InitializeComponent();
// make the clicks on the picturebox invoke the click event
// on our control
Loading_pb.Click += (o,e) => { this.OnClick(e); }
}
基本上,看起来你所缺少的是// you need to model the ETD element as that has the destination and etd elements
class Etd {
var destination: String = ""
var estimates = [String]()
init(destination: String, estimates: [String]) {
self.destination = destination
self.estimates = estimates
}
}
// each station has a collection of ETDs (per the XML)
class Station {
var name : String
var etds = [Etd]()
init(withName name: String, etds: [XMLIndexer]){
self.name = name
for etd in etds {
self.etds.append(Etd(
destination: etd["destination"].element!.text!,
estimates: etd["estimate"].all.map { $0["minutes"].element!.text! })
)
}
}
}
var stationsTest = [Station]()
for elem in xml["root"]["station"] {
var stations = elem["name"].element!.text!
var stationPlaceHolder = Station(withName: stations, etds: elem["etd"].all)
stationsTest.append(stationPlaceHolder)
}
类的抽象 - 一个站不包含目的地列表,而是一个Etd列表。然后,每个Etd
目标和估算值。