Advertisement
jumpy83

Untitled

Jan 14th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package com.example.corrado.nuovoprogetto.parser;
  2.  
  3. import org.jsoup.Jsoup;
  4. import org.jsoup.select.Elements;
  5. import org.w3c.dom.Document;
  6. import org.w3c.dom.Node;
  7. import org.w3c.dom.NodeList;
  8. import org.xml.sax.InputSource;
  9.  
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13. import javax.xml.parsers.DocumentBuilder;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15.  
  16. public class DOMParser {
  17.  
  18. private RSSFeed _feed = new RSSFeed();
  19.  
  20. public RSSFeed parseXml(String xml) {
  21.  
  22. // _feed.clearList();
  23.  
  24. URL url = null;
  25. try {
  26. url = new URL(xml);
  27. } catch (MalformedURLException e1) {
  28. e1.printStackTrace();
  29. }
  30.  
  31. try {
  32. // Create required instances
  33. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  34. DocumentBuilder db = dbf.newDocumentBuilder();
  35.  
  36. // Parse the xml
  37. Document doc = db.parse(new InputSource(url.openStream()));
  38. doc.getDocumentElement().normalize();
  39.  
  40. // Get all <item> tags.
  41. NodeList nl = doc.getElementsByTagName("item");
  42. int length = nl.getLength();
  43.  
  44. for (int i = 0; i < length; i++) {
  45. Node currentNode = nl.item(i);
  46. RSSItem _item = new RSSItem();
  47.  
  48. NodeList nchild = currentNode.getChildNodes();
  49. int clength = nchild.getLength();
  50.  
  51. // Get the required elements from each Item
  52. for (int j = 0; j < clength; j = j + 1) {
  53.  
  54. Node thisNode = nchild.item(j);
  55. String theString = null;
  56. String nodeName = thisNode.getNodeName();
  57.  
  58. theString = nchild.item(j).getFirstChild().getNodeValue();
  59.  
  60. if (theString != null) {
  61. if ("title".equals(nodeName)) {
  62. // Node name is equals to 'title' so set the Node
  63. // value to the Title in the RSSItem.
  64. _item.setTitle(theString);
  65. }
  66.  
  67. else if ("description".equals(nodeName)) {
  68. _item.setDescription(theString);
  69.  
  70. // Parse the html description to get the image url
  71. String html = theString;
  72. org.jsoup.nodes.Document docHtml = Jsoup
  73. .parse(html);
  74. Elements imgEle = docHtml.select("img");
  75. _item.setImage(imgEle.attr("src"));
  76. }
  77.  
  78. else if ("pubDate".equals(nodeName)) {
  79.  
  80. // We replace the plus and zero's in the date with
  81. // empty string
  82. String formatedDate = theString.replace(" +0000",
  83. "");
  84. _item.setDate(formatedDate);
  85. }
  86.  
  87. }
  88. }
  89.  
  90. // add item to the list
  91. _feed.addItem(_item);
  92. }
  93.  
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97.  
  98. // Return the final feed once all the Items are added to the RSSFeed
  99. // Object(_feed).
  100. return _feed;
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement