首页 > 代码库 > SAX解析XML

SAX解析XML

如果解析xml在主线程,则要在将xml解析成一个string,并进行解析;

主线程中更新UI的处理不应过多,可以另起方法;

否则会造成主线程中太多工作,而停止运行。

    private List<Mp3Info> parse(String xmlStr){    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();    List<Mp3Info> infos = new ArrayList<Mp3Info>();    try{    XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();    GetMp3Info getMp3Info = new GetMp3Info();    xmlReader.setContentHandler(getMp3Info);    xmlReader.parse(new InputSource(new StringReader(xmlStr)));    for (Iterator iterator = infos.iterator(); iterator.hasNext();) {    Mp3Info mp3Info = (Mp3Info) iterator.next();    Log.d("parse",mp3Info.getMp3name());    }    }    catch(Exception e){    e.printStackTrace();    }    return infos;    }

 

SAX解析XML