Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package com.supinfo.reader.sax;
  2.  
  3. import java.util.ArrayList;
  4. import org.xml.sax.Attributes;
  5. import org.xml.sax.SAXException;
  6. import org.xml.sax.helpers.DefaultHandler;
  7.  
  8. import android.net.Uri;
  9.  
  10. import com.supinfo.reader.beans.RSSItem;
  11.  
  12. public class RSSParser extends DefaultHandler {
  13.  
  14. private boolean currenttag;
  15. private String currentvalue;
  16. private ArrayList<RSSItem> listitems;
  17. private RSSItem item;
  18. private String name;
  19. private boolean isChannel;
  20.  
  21. public RSSParser() {
  22. currenttag = false;
  23. isChannel = false;
  24. listitems = new ArrayList<RSSItem>();
  25. }
  26.  
  27. @Override
  28. public void startElement(String uri, String localName, String qName,
  29. Attributes attributes) throws SAXException {
  30. // TODO Auto-generated method stub
  31. super.startElement(uri, localName, qName, attributes);
  32.  
  33. if (localName.equals("item")) {
  34. currenttag = true;
  35. item = new RSSItem();
  36. }
  37. else if (localName.equals("channel")) {
  38. isChannel = true;
  39. }
  40. }
  41.  
  42. @Override
  43. public void characters(char[] ch, int start, int length)
  44. throws SAXException {
  45. // TODO Auto-generated method stub
  46. super.characters(ch, start, length);
  47.  
  48. if (currenttag || isChannel) {
  49. currentvalue = new String(ch);
  50. }
  51. }
  52.  
  53. @Override
  54. public void endElement(String uri, String localName, String qName)
  55. throws SAXException {
  56. // TODO Auto-generated method stub
  57. super.endElement(uri, localName, qName);
  58.  
  59. if (currenttag && !currentvalue.isEmpty()) {
  60. if (localName.equals("title") && !isChannel) {
  61. item.setTitle(currentvalue);
  62. }
  63. else if (localName.equals("description")) {
  64. item.setDescription(currentvalue);
  65. }
  66. else if (localName.equals("link")) {
  67. item.setLink(Uri.parse(currentvalue));
  68. }
  69. else if (localName.equals("pubDate")) {
  70. item.setPublicationDate(currentvalue);
  71. }
  72. }
  73. else if (localName.equals("title") && isChannel) {
  74. name = currentvalue;
  75. isChannel = false;
  76. }
  77.  
  78. if (localName.equals("item")) {
  79. listitems.add(item);
  80. currenttag = false;
  81. }
  82. }
  83.  
  84. public ArrayList<RSSItem> getFlux() {
  85. return listitems;
  86. }
  87.  
  88. public String getName() {
  89. return name;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement