【发布时间】:2012-08-30 06:09:26
【问题描述】:
我正在开发一个 RESTful Web 服务,它会返回一个 RSS 提要列表,有人将这些提要列表添加到我之前实现的提要列表中。
现在,如果我返回 TEXT_PLAIN 回复,浏览器中会显示得很好,但当我尝试返回 APPLICATION_XML 回复时,会出现以下错误:
XML 解析错误:文档元素后出现垃圾 位置:http://localhost:8080/Assignment1/api/feedlist 第 1 行,第 135 列:SMH 头条新闻http://feeds.smh.com.au/rssheadlines/top.xmlUTS 图书馆新闻http://www.lib.uts.edu.au/news/feed/all
这是代码——我不知道为什么它没有返回格式良好的 XML 页面(我也尝试过用新行和空格(缩进)格式化 XML 回复——当然这不起作用):
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;
}
}
【问题讨论】: