我只想解析一个特定的River数据。 来自这个网站:http://www.arso.gov.si/xml/vode/hidro_podatki_zadnji.xml
我的代码摘录了所有里弗斯:
ParseApplication.java;
public class ParseApplication {
private String xmlData; // XML file we are processing
private ArrayList<Application> applications;
public ParseApplication(String xmlData) {
this.xmlData = xmlData;
this.applications = new ArrayList<Application>();
}
public ArrayList<Application> getApplications() {
return applications;
}
public boolean process()
{
boolean status = true;
Application currentRecord = null;
boolean inEntry = false;
String textValue = "";
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(this.xmlData));
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
String tagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
Log.d("ParseApplications", "Starting tag for " + tagName);
if (tagName.equalsIgnoreCase("postaja")) {
Log.d("ParseApplication", "Create application item");
inEntry = true;
currentRecord = new Application();
}
break;
case XmlPullParser.TEXT:
textValue = parser.getText(); // data itself, used in End_Tag
break;
case XmlPullParser.END_TAG:
Log.d("ParseApplication", "Ending tag for " + tagName);
if (inEntry) {
if (tagName.equalsIgnoreCase("postaja")) {
// Save the record and set entry to false
Log.d("ParseApplication", "Store Application With Name " + currentRecord.getReka());
this.applications.add(currentRecord);
inEntry = false;
} else if (tagName.equalsIgnoreCase("reka")) {
currentRecord.setReka(textValue); // TextValue already updated
} else if (tagName.equalsIgnoreCase("pretok")) {
currentRecord.setPretok(textValue);
} else if (tagName.equalsIgnoreCase("datum")) {
currentRecord.setDatum(textValue);
}
}
break;
default:
break;
}
eventType = parser.next();
}
} catch(Exception e) {
status = false;
e.printStackTrace();
}
return status;
}
}
和
application.java
public class Application {
private String reka;
private String pretok;
private String datum;
public String getReka() {
return reka;
}
public void setReka(String reka) {
this.reka = reka;
}
public String getPretok() {
return pretok;
}
public void setPretok(String pretok) {
this.pretok = pretok;
}
public String getDatum() {
return datum;
}
public void setDatum(String datum) {
this.datum = datum;
}
@Override
public String toString() {
return "Reka: " + this.getReka() + "\n" +
"Pretok: " + this.getPretok() + " m3\n" +
"Datum: " + this.getDatum();
}
}
如何仅解析一个特定的XML River数据?