xmlparser错误,startdocument结束文档功能

时间:2014-06-17 12:42:59

标签: java android

package adapters;


public class ParserResult extends Activity {
    TextView tv_question_1,tv_question_id1,tv_question_text1;
    String temp = null;
    String payload = "";
    String parsed_data = "";
    String parser_data = "";
    List<Question> myData = null;
    String question,question_id,question_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parser_result);
        tv_question_1 = (TextView) findViewById(R.id.tv_question_1);
        tv_question_id1 = (TextView) findViewById(R.id.tv_question_id1);
        tv_question_text1 = (TextView) findViewById(R.id.tv_question_text1);
        payload = new NewTask(getApplicationContext()).payload_data;


        try{
            StringReader sr = new StringReader(payload);
            payload = "<question id='56 'text='Favorite Food'><option id='1'>chinese</option><option id='2'>indian</option><option id='3'>thai</option></question>";
            BufferedReader br=new BufferedReader(new StringReader(payload));
            InputSource is=new InputSource(br);

            //Create XML Parser
            XMLParser parser=new XMLParser();
            SAXParserFactory factory=SAXParserFactory.newInstance();
            SAXParser sp=factory.newSAXParser();
            XMLReader reader=sp.getXMLReader();
            reader.setContentHandler(parser);
            reader.parse(is);
            int a = 3;          
//          tv_question_id1.setText(reader.toString());
//          myData=parser.question_list;
            if(myData!=null){

                String OutputData = "";

                for (Question xmlRowData : myData) {
                  if(xmlRowData!=null)
                  {  
                    tv_question_text1.setText("33"); 
                  }
                  else
                      Log.e("Jobs", "Jobs value null");
                }

                tv_question_1.setText(myData.toString());
            }
        }

        catch(Exception e){
            e.printStackTrace();
        }
    }


    public String getPayload() {
        return payload;
    }
    public void setPayload(String payload) {
        this.payload = payload;
    }


}

Xmlparser is not working.



package adapters;

public class XMLParser extends DefaultHandler{
    String localname = "";
    List<Question> question_list = null;
    Question question = new Question();
    OptionsParser option_parser = new OptionsParser();
    StringBuilder builder = new StringBuilder();
    List<OptionsParser> option_list = null;

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        option_list = new ArrayList<OptionsParser>();
        question_list = new ArrayList<Question>();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        int a = 22;
        if(localName == "question"){
            localname = localName;
            question = new Question();
            question.setQuestion_id(attributes.getValue("id"));
            question.setQuestion_text(attributes.getValue("text"));
            int length = attributes.getLength();
        }

        if(localName == "optoins"){
            localname = localName;
            option_parser = new OptionsParser();
            option_parser.setOption_id(attributes.getValue("id"));

        }


    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        if(localName.equalsIgnoreCase("question")){
            question_list.add(question);
        }

        if(localName.equalsIgnoreCase("option")){
             option_list.add(option_parser);

        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        String tempString=new String(ch, start, length);
        builder.append(tempString);
        if(localname == "option"){
        option_parser.setOption_text(builder.toString());
      }
    }
}

控件不是读取startelement函数。 控制不是阅读功能。 控制不是读取字符功能。 我不能在xmlparser类中出错,也不会显示错误。

0 个答案:

没有答案