/** * Parses the document using the expected RSS format. * * @return Feed */ @Override public Feed parse(Document document) { Feed feed = new Feed(); // Get the root node. Node rssElement = document.getFirstChild(); NamedNodeMap attrMap = rssElement.getAttributes(); feed.setVersion(attrMap.getNamedItem("version").getNodeValue()); // Now get channel. NodeList childNodes = rssElement.getChildNodes(); Node channelNode = null; for (int index = 0; index < childNodes.getLength(); index++) { Node childNode = childNodes.item(index); if (!childNode.getNodeName().equalsIgnoreCase("channel")) { continue; } channelNode = childNode; } if (channelNode == null) { throw new ParserException("Parser could not find the channel element in the RSS feed."); } // We've done enough here... return parseChannel(feed, channelNode); }
/** * Handles a Node (determining whether it is a title, description, item, etc. and handling it * appropriately). * * @param feed The feed to set any values to. * @param node The node. */ private void parseNode(Feed feed, Node node) { switch (node.getNodeName().toLowerCase()) { case "title": feed.setTitle(getNodeValue(node)); break; case "description": feed.setDescription(getNodeValue(node)); break; case "link": feed.setLink(getNodeValue(node)); break; case "lastbuilddate": feed.setUpdated(getNodeValue(node)); break; case "item": parseItem(feed, node); break; } }
/** * Parses a node representing an item. * * @param feed * @param node */ private void parseItem(Feed feed, Node node) { NodeList childNodes = node.getChildNodes(); FeedItem feedItem = new FeedItem(); for (int index = 0; index < childNodes.getLength(); index++) { Node childNode = childNodes.item(index); // We aren't interested in text nodes. if (childNode.getNodeType() == Node.TEXT_NODE) { continue; } parseItemNode(feedItem, childNode); } feed.getItems().add(feedItem); }