Android 从本地服务器下载文件与解析过程详解(三)

来源:互联网 发布:linux 发送http请求 编辑:程序博客网 时间:2024/06/15 06:03
2、解析文件需要的代码:

java代码:

  1. package eoe.xml;

  2. import java.util.List;
  3. import org.xml.sax.Attributes;
  4. import org.xml.sax.SAXException;
  5. import org.xml.sax.helpers.DefaultHandler;
  6. import com.pisces.model.Mp3Info;

  7. public class Mp3ListContentHandler extends DefaultHandler{
  8. private List<Mp3Info> infos=null;
  9. private Mp3Info mp3Info=null;
  10. private String tagName=null;
  11. @Override
  12. public void characters(char[] ch, int start, int length)
  13. throws SAXException {
  14. String temp=new String(ch,start,length);
  15. if(tagName.equals("id")){
  16. mp3Info.setId(temp);
  17. }else if(tagName.equals("mp3.name")){
  18. mp3Info.setMp3Name(temp);
  19. }else if(tagName.equals("mp3.size")){
  20. mp3Info.setMp3Size(temp);
  21. }else if(tagName.equals("lrc.name")){
  22. mp3Info.setLrcName(temp);
  23. }else if(tagName.equals("lrc.size")){
  24. mp3Info.setLrcSize(temp);
  25. }
  26. }
  27. @Override
  28. public void endDocument() throws SAXException {
  29. // TODO Auto-generated method stub
  30. super.endDocument();
  31. }
  32. @Override
  33. public void endElement(String uri, String localName, String qName)
  34. throws SAXException {
  35. if(localName.equals("resource")){
  36. //此部分应注意
  37. System.out.println("-----------");
  38. infos.add(mp3Info);
  39. }
  40. tagName="";
  41. }
  42. public Mp3ListContentHandler(List<Mp3Info> infos) {
  43. super();
  44. this.infos = infos;
  45. }
  46. public List<Mp3Info> getInfos() {
  47. return infos;
  48. }
  49. public void setInfos(List<Mp3Info> infos) {
  50. this.infos = infos;
  51. }
  52. @Override
  53. public void startDocument() throws SAXException {
  54. // TODO Auto-generated method stub
  55. super.startDocument();
  56. }
  57. @Override
  58. public void startElement(String uri, String localName, String qName,
  59. Attributes attributes) throws SAXException {
  60. // TODO Auto-generated method stub
  61. this.tagName=localName;
  62. if(tagName.equals("resource")){
  63. mp3Info=new Mp3Info();
  64. }
  65. }

  66. }
复制代码

原创粉丝点击