我正在尝试为 Android 制作一个 SAX 解析器,并从外部 XML 文件中仅读取一个标签开始。我收到“不幸的是,-- 已停止”错误。我查看了日志文件,它给了我一个空引用错误。我的猜测是 XMLAdapter 类不能正常工作,我无法解决问题。
这是我的主要活动:
package com.vint.michiganbus;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.view.Window;
import android.util.Log;
public class listView extends Activity {
XMLGettersSetters data;
private static final String TAG = "listView";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
View layout = findViewById(R.id.layout);
TextView title[];
Log.i(TAG, "data is hello");
try {
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("http://mbus.pts.umich.edu/shared/public_feed.xml"); // URL of the XML
/**
* Create the Handler to handle each of the XML tags.
**/
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
}
data = XMLHandler.data;
/**
* Makes the TextView length the size of the TextView arrays by getting the size of the
**/
title = new TextView[data.get().size()];
/**
* Run a for loop to set All the TextViews with text until
* the size of the array is reached.
*
**/
for (int i = 0; i < data.get().size(); i++) {
title[i] = new TextView(this);
title[i].setText("Title = "+data.get().get(i));
((ViewGroup) layout).addView(title[i]);
}
setContentView(layout);
//setContentView(R.layout.listview);
}
}
这是我的 XML 处理程序:
package com.vint.michiganbus;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XMLHandler extends DefaultHandler {
private static final String TAG = "In Handler";
String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;
public static XMLGettersSetters getXMLData() {
return data;
}
public static void setXMLData(XMLGettersSetters data) {
XMLHandler.data = data;
}
/**
* This will be called when the tags of the XML starts.
**/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementOn = true;
Log.i(TAG, "starting Element "+localName);
if (localName.equals("livefeed"))
{
data = new XMLGettersSetters();
} else if (localName.equals("routecount")) {
/**
* We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> )
* we can get the value "band". Below is an example of how to achieve this.
*
* String attributeValue = attributes.getValue("attr");
* data.setAttribute(attributeValue);
*
* */
}
}
/**
* This will be called when the tags of the XML end.
**/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase("routecount"))
data.set(elementValue);
/*else if (localName.equalsIgnoreCae("artist"))
data.setArtist(elementValue);
else if (localName.equalsIgnoreCase("country"))
data.setCountry(elementValue);
else if (localName.equalsIgnoreCase("company"))
data.setCompany(elementValue);
else if (localName.equalsIgnoreCase("price"))
data.setPrice(elementValue);
else if (localName.equalsIgnoreCase("year"))
data.setYear(elementValue);*/
}
/**
* This is called to get the tags value
**/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (elementOn) {
elementValue = new String(ch, start, length);
elementOn = false;
}
}
}
这是我的 GetterSetter 类
package com.vint.michiganbus;
import java.util.ArrayList;
public class XMLGettersSetters {
private ArrayList<String> routecount = new ArrayList<String>();
public ArrayList<String> get() {
return routecount;
}
public void set(String company) {
this.routecount.add(company);
}
}
任何帮助将不胜感激!
ps:如果有人对如何有效地处理 URL 中的 XML 有任何建议,我会很高兴的!