XMLPullParser 未找到 START_TAGs
XMLPullParser not finding START_TAGs
我正在尝试解析来自以下 URL
的 XML 数据
Traffic Scotland Current Incidents
现在,我有一个应用程序,按下按钮,获取 XML 数据,尝试解析它,然后只显示原始 xml 数据,因为我目前无法解析任何东西。
据我所知,下面的代码 应该 可以工作,但我不知道为什么不能。它似乎直接从 START_DOCUMENT(eventType 0)到 END_DOCUMENT(eventType 1)。
public void onClick(View view)
{
if (view == ciButton)
viewCI();
if (view == prButton)
{
viewPR();
}
}
public void viewCI()
{
new Thread(new Task(urlCI)).start();
}
public void viewPR()
{
new Thread(new Task(urlPR)).start();
}
class Task implements Runnable
{
private String url1;
private String text;
Incident incident = null;
ArrayList<Incident> incidents = null;
public Task (String aUrl)
{
url1 = aUrl;
}
@Override
public void run()
{
String inputLine;
Log.e("MyTag","in run");
try
{
Log.e("MyTag","in try");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL url = new URL(url1);
URLConnection uc = url.openConnection();
InputStreamReader isr = new InputStreamReader(uc.getInputStream());
BufferedReader in = new BufferedReader(isr);
int lineNum = 0;
//Skips first line of XML feed
while ((inputLine = in.readLine()) != null)
{
lineNum++;
if (lineNum > 1)
{
result = result + inputLine;
}
}
xpp.setInput(uc.getInputStream(),null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
Log.e("MyTag","in while");
Log.e("MyTag",Integer.toString(eventType));
if (eventType == XmlPullParser.START_DOCUMENT)
{
Log.e("MyTag","Start Doc");
}
else if (eventType == XmlPullParser.START_TAG)
{
String tag = xpp.getName();
Log.e("MyTag","Start tag: "+tag);
}
else if (eventType == XmlPullParser.END_TAG)
{
String tag = xpp.getText();
Log.e("MyTag","End Tag: "+tag);
}
else if (eventType == XmlPullParser.TEXT)
{
String tag = xpp.getText();
Log.e("MyTag","Text: "+tag);
}
Log.e("MyTag","xpp.next");
eventType = xpp.next();
Log.e("MyTag","ET after next: "+Integer.toString(eventType));
}
}
catch (XmlPullParserException xppe)
{
Log.e("MyTag","XPPE: ");
xppe.printStackTrace();
}
catch (IOException ioe)
{
Log.e("MyTag", "IOE");
ioe.printStackTrace();
}
MainActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run() {
Log.d("UI thread", "I am the UI thread");
urlInput.setText(result);
}
});
}
}
当我运行上面的代码时,我记录了以下内容:
02-21 09:17:52.201 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in run
02-21 09:17:52.232 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in try
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in
while
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: 0
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: Start Doc
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag:
xpp.next
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: ET after next: 1
xpp.setInput(uc.getInputStream(),null);
那是你第二次使用uc.getInputStream()
。您已经阅读了整个流。您已经在 String result
.
中获得了整个文档
您无法再次读取流。您无法再次阅读该文档。如果您尝试,它将立即指示流结束。
我正在尝试解析来自以下 URL
的 XML 数据Traffic Scotland Current Incidents
现在,我有一个应用程序,按下按钮,获取 XML 数据,尝试解析它,然后只显示原始 xml 数据,因为我目前无法解析任何东西。
据我所知,下面的代码 应该 可以工作,但我不知道为什么不能。它似乎直接从 START_DOCUMENT(eventType 0)到 END_DOCUMENT(eventType 1)。
public void onClick(View view)
{
if (view == ciButton)
viewCI();
if (view == prButton)
{
viewPR();
}
}
public void viewCI()
{
new Thread(new Task(urlCI)).start();
}
public void viewPR()
{
new Thread(new Task(urlPR)).start();
}
class Task implements Runnable
{
private String url1;
private String text;
Incident incident = null;
ArrayList<Incident> incidents = null;
public Task (String aUrl)
{
url1 = aUrl;
}
@Override
public void run()
{
String inputLine;
Log.e("MyTag","in run");
try
{
Log.e("MyTag","in try");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL url = new URL(url1);
URLConnection uc = url.openConnection();
InputStreamReader isr = new InputStreamReader(uc.getInputStream());
BufferedReader in = new BufferedReader(isr);
int lineNum = 0;
//Skips first line of XML feed
while ((inputLine = in.readLine()) != null)
{
lineNum++;
if (lineNum > 1)
{
result = result + inputLine;
}
}
xpp.setInput(uc.getInputStream(),null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
Log.e("MyTag","in while");
Log.e("MyTag",Integer.toString(eventType));
if (eventType == XmlPullParser.START_DOCUMENT)
{
Log.e("MyTag","Start Doc");
}
else if (eventType == XmlPullParser.START_TAG)
{
String tag = xpp.getName();
Log.e("MyTag","Start tag: "+tag);
}
else if (eventType == XmlPullParser.END_TAG)
{
String tag = xpp.getText();
Log.e("MyTag","End Tag: "+tag);
}
else if (eventType == XmlPullParser.TEXT)
{
String tag = xpp.getText();
Log.e("MyTag","Text: "+tag);
}
Log.e("MyTag","xpp.next");
eventType = xpp.next();
Log.e("MyTag","ET after next: "+Integer.toString(eventType));
}
}
catch (XmlPullParserException xppe)
{
Log.e("MyTag","XPPE: ");
xppe.printStackTrace();
}
catch (IOException ioe)
{
Log.e("MyTag", "IOE");
ioe.printStackTrace();
}
MainActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run() {
Log.d("UI thread", "I am the UI thread");
urlInput.setText(result);
}
});
}
}
当我运行上面的代码时,我记录了以下内容:
02-21 09:17:52.201 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in run
02-21 09:17:52.232 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in try
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: in while
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: 0
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: Start Doc
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: xpp.next
02-21 09:18:00.092 3258-3572/com.example.chdick.pullparsertest2 E/MyTag: ET after next: 1
xpp.setInput(uc.getInputStream(),null);
那是你第二次使用uc.getInputStream()
。您已经阅读了整个流。您已经在 String result
.
您无法再次读取流。您无法再次阅读该文档。如果您尝试,它将立即指示流结束。