如何在Android应用程序中解析三个xml文件

时间:2015-06-13 08:05:46

标签: java android xml

我对单个xml文件进行了解析,并在textview中显示了值。我尝试使用3个xml文件,但无法找到解析3个xml文件的确切方法,我无法找到问题所在。

下面提供了代码。任何人都可以提出建议。

tv = (TextView) findViewById(R.id.textView4);
tv1 = (TextView) findViewById(R.id.textView5);

try {
    SAXParserFactory factory = SAXParserFactory.newInstance();

    SAXParser saxParser = factory.newSAXParser();


    DefaultHandler handler = new DefaultHandler() {

        boolean name = false;

        boolean salary = false;


        public void startElement(String uri, String localName, String qName,
                                 Attributes attributes) throws SAXException {
            if (qName.equalsIgnoreCase("name")) {
                name = true;
            }
            if (qName.equalsIgnoreCase("salary")) {
                salary = true;
            }
        }//end of startElement method  

        public void endElement(String uri, String localName,
                               String qName) throws SAXException {
        }

        public void characters(char ch[], int start, int length) throws SAXException {
            if (name) {

                tv.setText(tv.getText() + "\n" + new String(ch, start, length));
                name = false;
            }
            if (salary) {
                tv1.setText(tv1.getText() + "\n" + new String(ch, start, length));
                salary = false;
            }
        }//end of characters  

    };//end of DefaultHandler object  

    InputStream is = getAssets().open("file.xml");
    saxParser.parse(is, handler);

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

0 个答案:

没有答案