1
votes

I am working on a RESTful web service, that will return a list of RSS feeds that someone has added to a feed list which I have previously implemented.

Now if I return a TEXT_PLAIN reply, this displays just fine in the browser, although when I attempt to return an APPLICATION_XML reply, then I get the following error:

XML Parsing Error: junk after document element Location: http:// localhost:8080/Assignment1/api/feedlist Line Number 1, Column 135:SMH Top Headlineshttp://feeds.smh.com.au/rssheadlines/top.xmlUTS Library Newshttp://www.lib.uts.edu.au/news/feed/all

Here is the code - I cannot figure out why it is not returning a well formed XML page (I have also tried formatting the XML reply with new lines and spaces(indents) - and of course this did not work):

package au.com.rest;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import au.edu.uts.it.wsd.*;


@Path("/feedlist")
public class RESTFeedService {

String feedFile = "/tmp/feeds.txt"; 
String textReply = "";
String xmlReply = "<?xml version=\"1.0\"?><feeds>";
FeedList feedList = new FeedListImpl();

@GET
@Produces(MediaType.APPLICATION_XML)
public String showXmlFeeds() throws FileNotFoundException, IOException
{

    feedList.load(feedFile);
    for (Feed f:feedList.list()){
        xmlReply += "<feed><name>" + f.getName() + "</name>";
        xmlReply += "<uri>" + f.getURI() + "</uri></feed></feeds>";
    }

    return xmlReply;
}

}
1

1 Answers

2
votes

EDIT: I've spotted the immediate problem now. You're closing the feeds element on every input element:

for (Feed f:feedList.list()){
    xmlReply += "<feed><name>" + f.getName() + "</name>";
    xmlReply += "<uri>" + f.getURI() + "</uri></feed></feeds>";
}

The minimal change would be:

for (Feed f:feedList.list()){
    xmlReply += "<feed><name>" + f.getName() + "</name>";
    xmlReply += "<uri>" + f.getURI() + "</uri></feed>";
}
xmlReply += "</feeds>";

... but you should still apply the rest of the advice below.


First step - you need to diagnose the problem further. Look at the source in the browser to see exactly what it's complaining about. Can you see the problem in the XML yourself? What does it look like?

Without knowing about the rest framework you're using, this looks like it could be a problem to do with a single instance servicing multiple requests. For some reason you've got an instance variable which you're mutating in your method. Why would you want to do that? If a new instance of your class is created for each request, it shouldn't be a problem - but I don't know if that's the case.

As a first change, try moving this line:

String xmlReply = "<?xml version=\"1.0\"?><feeds>";

into the method as a local variable.

After that though:

  • Keep all your fields private
  • Avoid using string concatenation in a loop like this
  • More importantly, don't build up XML by hand - use an XML API to do it. (The built-in Java APIs aren't nice, but there are plenty of alternatives.)
  • Consider which of these fields (if any) is really state of the object rather than something which should be a local variable. What state does your object logically have at all?