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

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. }
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值