Home > Programming > Java Parsing RSS XML using XPath

Java Parsing RSS XML using XPath

import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.xpath.*;
import javax.xml.parsers.*;

public class XmlParser {
 public static void main(String[] args) {
 try {
  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  URL url = new URL("http://www.somesite.com/index.php?format=feed&type=rss");
  InputStream inputStream = url.openStream();
  Reader reader = new InputStreamReader(inputStream, "UTF-8");
  InputSource inputSource = new InputSource(reader);
  Document doc = builder.parse(inputSource);
  XPath xpath = XPathFactory.newInstance().newXPath();
  XPathExpression expr = xpath.compile("//rss/channel/item/title/text()");
  NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
   for (int i = 0; i < nodes.getLength(); i++)
    String title = nodes.item(i).getNodeValue();
  catch (Exception exception) {

Categories: Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: