Solved

Parse syndication feed using jdom

Posted on 2011-03-24
5
491 Views
Last Modified: 2013-11-18
Hello,

Hello I have to parse a online feed using syndication and jdom. All I would need is to get the
epi:category element where I am unable using just syndication API.. Please help

Below is the java program I have used.
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.io.XmlReader;

import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
import org.jdom.*;

public class CheckFeed {

      public static void main(String[] args) {
            try {
                  //URL feedUrl = new URL("https://gmail.google.com/gmail/feed/atom");
                  URL feedUrl  = new URL("https://api.entertainment.com/AtomServer3/feeds/category?location=48084");

                  HttpURLConnection httpcon = (HttpURLConnection)feedUrl.openConnection();
                  String encoding = new sun.misc.BASE64Encoder().encode("demo:demo".getBytes());
                  httpcon.setRequestProperty ("Authorization", "Basic " + encoding);
                  SyndFeedInput input = new SyndFeedInput();
                  SyndFeed feed = input.build(new XmlReader(httpcon));
                  //String value = ((SyndEntry) feed.getEntries().get(0)).getDescription().getValue();
                  //SyndFeed feed = input.build(new XmlReader(feedUrl));

                  //System.out.println("Feed:value " + value);
                  System.out.println("Title: " + feed.getTitle());
                  System.out.println("Feed Author: "+ feed.getAuthors());
                  
                  
                  SyndFeedOutput outputjdom = new SyndFeedOutput();
                  // will output feed as jdom
                  Document doc = outputjdom.outputJDom(feed);
                  //SAXBuilder builder = new SAXBuilder();
                  //Document doc = builder.build((File) feed);
                  XPath x      = XPath.newInstance("/feed/entry");
                  List list    = x.selectNodes(doc);
                  Iterator it2 = list.iterator();
                  while (it2.hasNext()) {
                        System.out.println("Content:"+it2.toString());
                  }
                  //listElements(list, "");
                  //doc.toString() is part of jdom should output the document to console            
                  System.out.println("Document::"+doc.toString());                  

                  Element root = doc.getRootElement();
                  System.out.println("content size is "+root.getContentSize());                  
                  
                  
                  //List entries = feed.getEntries();
                  //System.out.println("Total Entries : " + entries.size());
            } catch (Exception ex) {
                  ex.printStackTrace();
                  System.out.println("ERROR: " + ex.getMessage());
            }
      }

}

Below is the feed XML if I access through browser manually
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:epi_ps="https://api.entertainment.com/AtomServer3/doc/index.html#psReference" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:epi="https://api.entertainment.com/AtomServer3/doc/index.html#epiReference" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:gml="http://www.opengis.net/gml" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
  <author>
    <name>EPI</name>
  </author>
  <id>1300804687145</id>
  <entry>
    <title>Categories</title>
    <id>1300804687145</id>
    <updated>2011-03-22T14:38:07Z</updated>
    <epi:category id="160" name="RedemptionMethod" value="Online Discounts" records="18" />
    <epi:category id="159" name="RedemptionMethod" value="Printable Discounts" records="2644" />
    <epi:category id="161" name="AlphabetIndex" value="0-9" records="0" />
    <epi:category id="131" name="AlphabetIndex" value="A" records="0" />
    <epi:category id="132" name="AlphabetIndex" value="B" records="0" />
    <epi:category id="133" name="AlphabetIndex" value="C" records="0" />
    <epi:category id="134" name="AlphabetIndex" value="D" records="0" />
    <epi:category id="135" name="AlphabetIndex" value="E" records="0" />
    <epi:category id="136" name="AlphabetIndex" value="F" records="0" />
    <epi:category id="137" name="AlphabetIndex" value="G" records="0" />
    <epi:category id="138" name="AlphabetIndex" value="H" records="0" />
    <epi:category id="139" name="AlphabetIndex" value="I" records="0" />
    <epi:category id="140" name="AlphabetIndex" value="J" records="0" />
    <epi:category id="141" name="AlphabetIndex" value="K" records="0" />
    <epi:category id="142" name="AlphabetIndex" value="L" records="0" />
    <epi:category id="143" name="AlphabetIndex" value="M" records="0" />
    <epi:category id="144" name="AlphabetIndex" value="N" records="0" />
    <epi:category id="145" name="AlphabetIndex" value="O" records="0" />
    <epi:category id="146" name="AlphabetIndex" value="P" records="0" />
    <epi:category id="147" name="AlphabetIndex" value="Q" records="0" />
    <epi:category id="148" name="AlphabetIndex" value="R" records="0" />
    <epi:category id="149" name="AlphabetIndex" value="S" records="0" />
    <epi:category id="150" name="AlphabetIndex" value="T" records="0" />
    <epi:category id="151" name="AlphabetIndex" value="U" records="0" />
    <epi:category id="152" name="AlphabetIndex" value="V" records="0" />
    <epi:category id="153" name="AlphabetIndex" value="W" records="0" />
    <epi:category id="154" name="AlphabetIndex" value="X" records="0" />
    <epi:category id="155" name="AlphabetIndex" value="Y" records="0" />
    <epi:category id="156" name="AlphabetIndex" value="Z" records="0" />
    <epi:category id="25" name="Category" value="Dining" records="1629" />
    <epi:category id="26" name="Category" value="Events &amp; Attractions" records="287" />
    <epi:category id="27" name="Category" value="Shopping" records="704" />
    <epi:category id="91" name="Category" value="Travel" records="42" />
    <epi:category id="80" name="SubCategory" value="Informal &amp; Carryout" records="981" />
    <epi:category id="39" name="SubCategory" value="Casual Dining" records="565" />
    <epi:category id="42" name="SubCategory" value="Fine Dining" records="83" />
    <epi:category id="56" name="SubCategory" value="Attractions" records="16" />
    <epi:category id="65" name="SubCategory" value="Automotive" records="141" />
    <epi:category id="58" name="SubCategory" value="Bowling" records="53" />
    <epi:category id="84" name="SubCategory" value="Car Rental" records="18" />
    <epi:category id="32" name="SubCategory" value="Dry Cleaning" records="33" />
    <epi:category id="45" name="SubCategory" value="Golf" records="51" />
    <epi:category id="40" name="SubCategory" value="Grocery" records="11" />
    <epi:category id="81" name="SubCategory" value="Health &amp; Beauty" records="50" />
    <epi:category id="82" name="SubCategory" value="Home &amp; Garden" records="291" />
    <epi:category id="86" name="SubCategory" value="Hotel" records="24" />
    <epi:category id="64" name="SubCategory" value="Live Events" records="17" />
    <epi:category id="49" name="SubCategory" value="Museums" records="11" />
    <epi:category id="73" name="SubCategory" value="Music/Books/Video" records="5" />
    <epi:category id="62" name="SubCategory" value="Retail" records="156" />
    <epi:category id="90" name="SubCategory" value="Special Events" records="9" />
    <epi:category id="63" name="SubCategory" value="Sporting Events" records="34" />
    <epi:category id="34" name="SubCategory" value="Miscellaneous" records="94" />
    <epi:category id="471" name="SubCategory" value="Movies" records="11" />
    <epi:category id="485" name="SubCategory" value="Services" records="91" />
    <epi:category id="168" name="CuisineType" value="American" records="689" />
    <epi:category id="182" name="CuisineType" value="Asian" records="46" />
    <epi:category id="184" name="CuisineType" value="Cajun/Creole" records="4" />
    <epi:category id="185" name="CuisineType" value="Canadian" records="23" />
    <epi:category id="179" name="CuisineType" value="Coffee House" records="43" />
    <epi:category id="180" name="CuisineType" value="European" records="26" />
    <epi:category id="176" name="CuisineType" value="Indian" records="16" />
    <epi:category id="170" name="CuisineType" value="Italian" records="251" />
    <epi:category id="171" name="CuisineType" value="Mexican" records="45" />
    <epi:category id="178" name="CuisineType" value="Middle Eastern" records="20" />
    <epi:category id="469" name="OfferType" value="Frequent Value" records="597" />
    <epi:category id="470" name="OfferType" value="Premium" records="2064" />
  </entry>
</feed>
0
Comment
Question by:jexpert7
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35208263
Try
import org.jdom.*;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

import org.jdom.xpath.XPath;

import java.io.File;

import java.net.HttpURLConnection;
import java.net.URL;

import java.util.Iterator;
import java.util.List;


public class CheckFeed {
    @SuppressWarnings("all")
	public static void main(String[] args) {
		try {
			URL feedUrl = new URL(
					"https://api.entertainment.com/AtomServer3/feeds/category?location=48084");

			HttpURLConnection httpcon = (HttpURLConnection) feedUrl.openConnection();
			String encoding = new sun.misc.BASE64Encoder().encode("demo:demo".getBytes());
			httpcon.setRequestProperty("Authorization", "Basic " + encoding);


			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(httpcon.getInputStream());
			XPath x = XPath.newInstance("//epi:category");
			List list = x.selectNodes(doc);
			Iterator it2 = list.iterator();

			while (it2.hasNext()) {
				System.out.println("Content:" + it2.next().toString());
			}

			System.out.println("Document::" + doc.toString());

			Element root = doc.getRootElement();
			System.out.println("content size is " + root.getContentSize());

		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("ERROR: " + ex.getMessage());
		}
	}
}

Open in new window

0
 

Author Comment

by:jexpert7
ID: 35209080
Thanks a lot CEHJ,

One last thing, this will be easy for you, I have to write a XPATH statement to get the count of "records" value for epi:category id="159" or id="160". what should it be?

    <epi:category id="160" name="RedemptionMethod" value="Online Discounts" records="18" />
    <epi:category id="159" name="RedemptionMethod" value="Printable Discounts" records="2644" />
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 35209272
You can do that thus:
XPath x = XPath.newInstance("//epi:category");
			List list = x.selectNodes(doc);
			Iterator it2 = list.iterator();

			while (it2.hasNext()) {
				//System.out.println("Content:" + it2.next().toString());
				Element e = (Element)it2.next();
				System.out.println(e.getAttributeValue("records"));
			}

Open in new window

0
 

Author Comment

by:jexpert7
ID: 35209333
Super, you are the best!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35209346
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java passing arguments (type error) 2 48
sites similar to codingbat to improve coding hanson skills 3 41
rhino JavaScript import, load 25 69
egit plugin on eclipse 8 40
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now