Skip to content

Instantly share code, notes, and snippets.

@HituziANDO
Created August 29, 2012 03:29
Show Gist options
  • Save HituziANDO/3506521 to your computer and use it in GitHub Desktop.
Save HituziANDO/3506521 to your computer and use it in GitHub Desktop.
Java_EkiDataApi
public class NearestSta
{
Coord coord;
List<Station> station = new ArrayList<Station>();
}
public class Coord
{
public float lon; // Request longitude
public float lat; // Request latitude
public int error; // Error code (0:OK / 1:ERROR)
}
public class Station
{
public long line_cd;
public String line_name;
public long station_cd;
public String station_name;
public float lon; // at station
public float lat; // at station
public float meter; // Straight distance from specified point to station
}
private NearestSta parseXML(String xml) throws XmlPullParserException, IOException
{
NearestSta near = new NearestSta();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(xml));
while(xpp.getEventType() != XmlPullParser.END_DOCUMENT)
{
// Start tag is <coord>
if(xpp.getEventType() == XmlPullParser.START_TAG && "coord".equals(xpp.getName()))
{
Coord coord = new Coord();
while(true)
{
int eventType = xpp.next(); // Move to next event
String name = xpp.getName(); // Get name of tag
// End tag is </coord>
if(eventType == XmlPullParser.END_TAG && "coord".equals(name)) break;
if(eventType == XmlPullParser.START_TAG)
{
if("lon".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { coord.lon = Long.parseLong(xpp.getText()); }
}
else if("lat".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { coord.lat = Long.parseLong(xpp.getText()); }
}
else if("error".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { coord.error = Integer.parseInt(xpp.getText()); }
}
}
}
near.coord = coord;
}
// Start tag is <station>
else if(xpp.getEventType() == XmlPullParser.START_TAG && "station".equals(xpp.getName()))
{
Station station = new Station();
while(true)
{
int eventType = xpp.next(); // Move to next event
String name = xpp.getName(); // Get name of tag
// End tag is </station>
if(eventType == XmlPullParser.END_TAG && "station".equals(name)) break;
if(eventType == XmlPullParser.START_TAG)
{
if("line_cd".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.line_cd = Long.parseLong(xpp.getText()); }
}
else if("line_name".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.line_name = xpp.getText(); }
}
else if("station_cd".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.station_cd = Long.parseLong(xpp.getText()); }
}
else if("station_name".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.station_name = xpp.getText(); }
}
else if("lon".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.lon = Float.parseFloat(xpp.getText()); }
}
else if("lat".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.lat = Float.parseFloat(xpp.getText()); }
}
else if("meter".equals(name))
{
if(xpp.next() == XmlPullParser.TEXT) { station.meter = Float.parseFloat(xpp.getText()); }
}
}
}
near.station.add(station);
}
xpp.next(); // Move to next event
}
return near;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment