我是編程中的新手,爲了更好地提高我對android中解析xml的理解,我試圖從web服務中獲取一些關於地震的數據。我已經閱讀了Stackoverflow和其他教程中的所有相關主題,但是我的代碼中存在錯誤。 下面是從XML的摘錄我想分析:用xmlpullparser寫入Xml文件夾
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<q:quakeml xmlns:q="http://quakeml.org/xmlns/quakeml/1.2" xmlns="http://quakeml.org/xmlns/bed/1.2" xmlns:ingv="http://webservices.ingv.it/fdsnws/event/1">
<eventParameters publicID="smi:webservices.ingv.it/fdsnws/event/1/query">
<event publicID="smi:webservices.ingv.it/fdsnws/event/1/query?eventId=16736781">
<type>earthquake</type>
<description>
<type>region name</type>
<text>Tirreno Meridionale (MARE)</text>
</description>
<preferredMagnitudeID>smi:webservices.ingv.it/fdsnws/event/1/query?magnitudeId=49992691</preferredMagnitudeID>
<preferredOriginID>smi:webservices.ingv.it/fdsnws/event/1/query?originId=49089691</preferredOriginID>
<creationInfo>
<agencyID>INGV</agencyID>
<author>SURVEY-INGV</author>
<creationTime>2017-08-16T14:59:48</creationTime>
</creationInfo>
而下面是我寫的代碼部分:
private static List<Earthquake> extractFeatureFromQuakeml(String earthquakeXML) throws XmlPullParserException, IOException {
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
xmlFactoryObject.setNamespaceAware(true);
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setInput(new StringReader(earthquakeXML));
// If the JSON string is empty or null, then return early.
if (TextUtils.isEmpty(earthquakeXML)) {
return null;
}
// Create an empty ArrayList that we can start adding earthquakes to
List<Earthquake> earthquakes = new ArrayList<>();
try {
int eventType = myparser.getEventType();
Log.d("Step", "Started parsing");
while (eventType != XmlPullParser.END_DOCUMENT) {
String location = "";
String time = "";
double magnitude = 0.0;
String url = "";
String tag = myparser.getName();
String text = "";
switch (eventType) {
case XmlPullParser.START_TAG:
//Log.d("Step","Started parsing, end tag: " + tag);
if (tag.equalsIgnoreCase("event")) {
url = myparser.getAttributeValue(null, "publicID");
Log.d(LOG_TAG, "Url" + url);
myparser.next();
}
break;
case XmlPullParser.END_TAG:
text = myparser.getText();
if (text != null) {
if (tag.equalsIgnoreCase("type") && text.equalsIgnoreCase("region name")) {
myparser.nextText();
location = "" + myparser.getText();
Log.d(LOG_TAG, "Location" + location);
}
}
if (tag.equalsIgnoreCase("creationTime")) {
time = "" + myparser.getText();
Log.d(LOG_TAG, "Time" + time);
}
if (tag.equalsIgnoreCase("mag")) {
myparser.next();
myparser.next();
String smagnitude = myparser.getText();
Log.d(LOG_TAG, "Magnitude" + smagnitude);
}
if (tag.equalsIgnoreCase("creationTime")) {
time = "" + myparser.getText();
Log.d(LOG_TAG, "Time" + time);
}
if (tag.equalsIgnoreCase("mag")) {
myparser.next();
myparser.next();
String smagnitude = myparser.getText();
Log.d(LOG_TAG, "Magnitude" + smagnitude);
}
break;
}
// Create a new {@link Earthquake} object with the magnitude, location, time,
// and url from the JSON response.
if (location != null && magnitude != 0.0 && time != null && url != null) {
Earthquake earthquake = new Earthquake(magnitude, location, time, url);
// Add the new {@link Earthquake} to the list of earthquakes.
earthquakes.add(earthquake);
}
eventType = myparser.next();
}
} catch (XmlPullParserException e) {
Log.e("QueryUtils", "Problem parsing the earthquake XML results", e);
}
// Return the list of earthquakes
return earthquakes;
}
現在我得到的唯一的價值是url,其他變量始終爲空。 有人可以指出我的代碼中的缺陷嗎?並且請溫柔一點,我已經開始編碼僅僅3周前:)
非常感謝!
而不是使用XmlPullParserFactory和XmlPullParser。您可以使用XML轉換器來使用Retrofit庫。請整合翻新並遵循https://futurestud.io/tutorials/retrofit-how-to-integrate-xml-converter – Rajesh