Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default RSS XML Parsing with XPath

    Hello all,

    I have a method shown below that is failing at the point of the xpath evaluation constructor consisting of (expression, InputSource, qname) -
    Java Code:
    Node node = (Node) xpath.evaluate("//item", is, XPathConstants.NODE);
    If I change it out and use the alternate constructor (expression, InputSource) that returns a String, then it works -
    Java Code:
    String node = xpath.evaluate("//item", is);
    I just can't seem to get it to return the node, though...

    I'm including the two difference versions of the method below, one that returns the Node (doesn't work) and one with the String(does work):

    Java Code:
    private Node getFeed(String urlToRssFeed, Context context)
        {
        	try
        	{
        		// setup the url
        		URL url = new URL(urlToRssFeed);
        		
    			XPath xpath = XPathFactory.newInstance().newXPath();
    			
    			URLConnection con = url.openConnection();
    			con.setConnectTimeout(15000);
    			con.setReadTimeout(15000);
    			InputSource is = new InputSource(con.getInputStream());
    			
    			Node node = (Node) xpath.evaluate("//item", is, XPathConstants.NODE);
    			
    			return node;
        	}
    	    	catch (Exception ee)
        	{
        		Log.d("RyanDebug", ee.getMessage());
        		return null;
        	}
        }
    Java Code:
    private String getFeed(String urlToRssFeed, Context context)
        {
        	try
        	{
        		// setup the url
        		URL url = new URL(urlToRssFeed);
        		
    			XPath xpath = XPathFactory.newInstance().newXPath();
    			
    			URLConnection con = url.openConnection();
    			con.setConnectTimeout(15000);
    			con.setReadTimeout(15000);
    			InputSource is = new InputSource(con.getInputStream());
    			
    			String string = xpath.evaluate("//item", is);
    			
    			return string;
        	}
    	    	catch (Exception ee)
        	{
        		Log.d("RyanDebug", ee.getMessage());
        		return null;
        	}
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: RSS XML Parsing with XPath

    Is the same data being returned each time through the connection?
    What happens in the first case if you replace NODE with STRING (and change the cast to a String)?

    What happens if you read the data returned in the connection stream and save it as a file, and use a FileInputStream from that instead (which is what I'd do to ensure a consistent test environment)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Re: RSS XML Parsing with XPath

    Quote Originally Posted by Tolls View Post
    Is the same data being returned each time through the connection?
    What happens in the first case if you replace NODE with STRING (and change the cast to a String)?

    What happens if you read the data returned in the connection stream and save it as a file, and use a FileInputStream from that instead (which is what I'd do to ensure a consistent test environment)?
    Thank you for the reply!
    Turns out that it was actually working, Eclipse just seems to be acting very oddly. During the debugging session, it would evaluate and then jump to the "return null;" in the exception catch. If you continue on from there, the program acts as if it evaluated "return string;" successfully. Very odd... but it works, anyway.

Similar Threads

  1. Replies: 0
    Last Post: 04-03-2011, 01:15 AM
  2. Parsing XML file using Xpath in jdk1.4
    By ziggy in forum New To Java
    Replies: 7
    Last Post: 02-27-2011, 12:55 PM
  3. Problems with parsing using XPath
    By thooom in forum XML
    Replies: 6
    Last Post: 04-26-2010, 09:47 AM
  4. Problems with XPath (XML Parsing)
    By thooom in forum New To Java
    Replies: 6
    Last Post: 04-25-2010, 04:56 PM
  5. Replies: 3
    Last Post: 09-11-2008, 03:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •