Question

I am trying to read an external rss feed using a xAgent, I seem to be gettig xml errors like the once below, I suspect it is because I read the xml line by line using the bufferedReader

I get errors like these

  • "error on line 1 at column 32: Extra content at the end of the document"
  • "error on line 1 at column 6561: Opening and ending tag mismatch: item line 0 and channel"

here is what I have tried

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse>
<![CDATA[#{javascript:

var u:java.net.URL  = new java.net.URL("http://www.xpages.info/XPagesHome.nsf/NewsFeed.xsp?format=rss");
var br:BufferedReader= new java.io.BufferedReader(new java.io.InputStreamReader(u.openStream()));
var tmp="";
while(br.readLine() != null){
    tmp+= br.readLine();
}

var externalContext = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = externalContext.getResponse();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
writer.write(tmp);
writer.endDocument();
}]]>

</xp:this.afterRenderResponse>
</xp:view>
Was it helpful?

Solution

You are using the XAgent as a proxy. In case you want to actually do more I suggest to use better classes. The minimum would be the HTTP client (handles all the connection nastiness) or if your source is ATOM, the Apache Abdera classes (which under the hood use the HTTP client).

The issue you have: the stream contains also the header information, so your XML doesn't start with <. Using the HTTP client you can avoid that.

A little bit like this:

public String getURL(String url) {

    System.out.println("Fetching " + url);

    if (this.httpClient == null) {
        this.initializeHTTPSession();
    }

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    HttpGet get = new HttpGet(url);

    String result = null;

    try {
        result = this.httpClient.execute(get, responseHandler);
    } catch (HttpResponseException e) {
        System.out.println(e.getMessage());
        return null;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        result = "The host is invalid: " + url;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

Full source code here: http://www.wissel.net/blog/downloads/SHWL-8BQPJD/$File/HTTPReader.java

You might want to use something else than a String for the response handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top