我创建了一个应用程序来使用 SAXParser 解析 XML 文件,代码如下。
以下是我的主要活动的代码。
public class XMLAppActivity extends Activity{
ArrayList<Question> questionList = new ArrayList<Question>();
TextView tvXmlReader;
InputSource xmlSource;
InputStream xmlIs;
String xmlFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvXmlReader=(TextView)findViewById(R.id.my_xml);
try
{
xmlIs= getAssets().open("surveyquestions.xml");
xmlFile = convertStreamToString(xmlIs);
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
xmlSource= new InputSource(new StringReader(xmlFile));
tvXmlReader.setText(parseXML().toString());
}
private String parseXML() {
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
QuestionXMLHandler myXMLHandler = new QuestionXMLHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(xmlSource);
questionList= myXMLHandler.getQuestionList();
return "Success";
}
catch (Exception e) {
return "Failure due to " + e.toString();
}
}
public String convertStreamToString(InputStream is)
throws IOException {
Writer writer = new StringWriter();
char[] buffer = new char[2048];
try {
Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String text = writer.toString();
return text;
}
}
这是我的 XMLhandler 的代码。
public class QuestionXMLHandler extends DefaultHandler {
private ArrayList<Question> questionL= new ArrayList<Question>();
Question cQuestion;
Boolean cElement = false;
String cValue = "";
public ArrayList<Question> getQuestionList() {
return questionL;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
cElement = true;
cValue = "";
// if current element is Question , create new question
// clear tmpValue on start of element
if (qName.equalsIgnoreCase("Question")) {
cQuestion = new Question();
cQuestion.setQuestionId(attributes.getValue("id"));
cQuestion.setQuestionName(attributes.getValue("name"));
cQuestion.setQuestionType(attributes.getValue("type"));
cQuestion.setQuestionIsRequired(Boolean.parseBoolean(attributes.getValue("isRequired")));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("Question")) {
questionL.add(cQuestion);
}
if (qName.equalsIgnoreCase("QuestionText")) {
cQuestion.setQuestionText(cValue);
}
if(qName.equalsIgnoreCase("Answer")){
cQuestion.getAnswers().add(cValue);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
cValue = new String(ch,start,length);
}
}
这是我的问题课。
public class Question
{
private String qId;
private String qName;
private String qType;
private String qText;
private Boolean isRequired;
private String qSurveyId;
private List<String> answers;
public Question()
{
qId="";
qName="";
qType="";
qText="";
isRequired=false;
qSurveyId="";
}
//getters and setter methods
}
但我收到以下错误:
06-27 14:44:36.148: W/System.err(591): java.lang.NullPointerException
06-27 14:44:36.187: W/System.err(591): at com.optimus.mobile.xml.QuestionXMLHandler.endElement(QuestionXMLHandler.java:52)
06-27 14:44:36.187: W/System.err(591): at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:160)
06-27 14:44:36.187: W/System.err(591): at org.apache.harmony.xml.ExpatParser.append(Native Method)
06-27 14:44:36.187: W/System.err(591): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:505)
06-27 14:44:36.197: W/System.err(591): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492)
06-27 14:44:36.197: W/System.err(591): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308)
06-27 14:44:36.197: W/System.err(591): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264)
06-27 14:44:36.197: W/System.err(591): at com.optimus.mobile.xml.XMLAppActivity.parseXML(XMLAppActivity.java:68)
06-27 14:44:36.197: W/System.err(591): at com.optimus.mobile.xml.XMLAppActivity.onCreate(XMLAppActivity.java:52)
06-27 14:44:36.197: W/System.err(591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-27 14:44:36.197: W/System.err(591): at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 14:44:36.207: W/System.err(591): at android.os.Looper.loop(Looper.java:123)
06-27 14:44:36.207: W/System.err(591): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-27 14:44:36.207: W/System.err(591): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 14:44:36.207: W/System.err(591): at java.lang.reflect.Method.invoke(Method.java:521)
06-27 14:44:36.207: W/System.err(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-27 14:44:36.207: W/System.err(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-27 14:44:36.207: W/System.err(591): at dalvik.system.NativeStart.main(Native Method)