-4

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)
4

3 に答える 3

1

間違いに気づきました!! これは、XML ハンドラー クラスの End Element メソッドにありました。基本的に、以下の行に問題があります。if(qName.equalsIgnoreCase("Answer")) { cQuestion.getAnswers().add(cValue); }

私のために働いた

于 2012-10-19T05:46:28.593 に答える
1

これを試して

xmlIs= this.getResources().getAssets().open("surveyquestions.xml");

それ以外の

xmlIs= getAssets().open("surveyquestions.xml");
于 2012-06-27T07:51:55.187 に答える
0

questionLあなたのコードでは" " が初期化されていないと思います......

以下のコードで使用されます.......

 @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);
            }

    }
于 2012-06-27T07:58:59.547 に答える