访问 RSS 源时出现奇怪的空格错误

发布于 2024-11-27 04:38:17 字数 3040 浏览 1 评论 0原文

我不确定之前是否有其他人遇到过或询问过此问题,但对于我的应用程序,我使用了两个 Yahoo! RSS 源:热门新闻和天气预报。我对首先使用这些的想法很陌生,但根据我所读到的内容,我只需要向特定 URL 发出 HTTP GET 请求即可检索 XML 文件,我可以解析该文件以获取我想要的信息。我的解析器工作得很好,因为我使用每个 feed 中的示例 XML 文件对其进行了测试;但是,当我使用 AJAX GET 调用 url 时,出现了一个奇怪的错误:

无法显示 XML 页面 无法使用 XSL 样式表查看 XML 输入。请更正错误,然后单击“刷新”按钮,或稍后重试。


Whitespace is not allowed at this location. 
Error processing resource 'http://localhost:8080/BBS/fservlet?p=n'. Line 28, P...

    for (i = 0; i < s.length; i++){
-------------------^

请注意,我当前已使用 Tomcat 在本地系统上部署了此应用程序“BBS”​​。我研究了类似这样的空白错误,大多数似乎都指向 XML 文件本身中存在问题的某些行。在大多数情况下,这与转义“&”有关。符号,但 IE 似乎告诉我错误是在 for 循环内。我不是 XML 专家,但我从未见过 XML 中的 for 循环。即便如此,我还是直接在浏览器中访问了该 url,并查看了 XML 文件(我用来测试解析的文件),但没有发现这样的行。此外,我的代码中的任何地方都不存在这样的循环。换句话说,我不确定这是我的错误还是某些配置设置。不过,这是我正在使用的代码:

jQuery 代码

// Located in my JSP file
var baseContext = "<%=request.getContextPath()%>";
$(document).ready(function() {
    ParseWeather();
    ParseNews();
}

// Located in a separate JS file
function ParseWeather() {
    $.get(baseContext + "/servlet?p=w", function(data) {
        // XML Parser
    }
    // Data Manipulation
}

function ParseNews() {
    $.get(baseContext + "/servlet?p=n", function(data) {
        // XML Parser
    }
    // Data Manipulation
}

Java 代码

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import java.net.URL;

public class FeedServlet extends HttpServlet {
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        try {
            response.setContentType("text/xml");
            final URL url;
            String line = "";

            if(request.getParameter("p").equals("w")) {
                // Configuration setting that returns: "http://xml.weather.yahoo.com/forecastrss?p=USOR0186"
                url = new URL(AppConfiguration.getInstance().getForcastUrl());
            } else {
                // Configuration setting that returns: "http://news.yahoo.com/rss/"
                url = new URL(AppConfiguration.getInstance().getNewsUrl());
            }
            final BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream());
            final PrintWriter writer = response.getWriter();

            while((line = reader.readLine()) != null) {
                writer.println(line);
                writer.flush();
            }
            writer.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

我的公司有一个 AppConfiguration 类,它允许通过配置页面更改某些变量(例如 URL)。无论如何,这两个调用简单地返回 url...

Yahoo!预测 RSS 源: http://xml.weather.yahoo.com/forecastrss?p=USOR0186

雅虎!新闻:头条新闻提要: http://news.yahoo.com/rss/

无论如何,任何帮助都会非常有帮助。

I'm not sure if anyone else has encountered or asked about this before, but for my application I make use of two Yahoo! RSS Feeds: Top News and Weather Forcast. I'm new to the idea of using these in the first place, but from what I've read, I simply need to make an HTTP GET request to a specific URL to retrieve an XML file which I can parse for the information I want. I have the parser working just fine, for I tested it with a sample XML file from each feed; however, a strange error is occuring when I use the AJAX GET call to the urls:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


Whitespace is not allowed at this location. 
Error processing resource 'http://localhost:8080/BBS/fservlet?p=n'. Line 28, P...

    for (i = 0; i < s.length; i++){
-------------------^

Note that I have this applciation "BBS" currently deployed on my local system with Tomcat. I looked into whitespace errors like this, and most seem to point to some line within the XML file itself that's having a problem. In most cases, it had something to do with escaping the "&" symbol, but it appears as though IE is telling me that the error is within a for-loop. I'm no XML expert, but I've never seen a for-loop within an XML. Even so, I've gone to the url directly in my browser and viewed the XML file (its the one I used to test my parsing) and found no such line. In addition, no such loop exists anywhere in my code. In other words, I'm not sure if this is an error on my end, or some configuration setting. Here's the code I'm working with, however:

jQuery Code

// Located in my JSP file
var baseContext = "<%=request.getContextPath()%>";
$(document).ready(function() {
    ParseWeather();
    ParseNews();
}

// Located in a separate JS file
function ParseWeather() {
    $.get(baseContext + "/servlet?p=w", function(data) {
        // XML Parser
    }
    // Data Manipulation
}

function ParseNews() {
    $.get(baseContext + "/servlet?p=n", function(data) {
        // XML Parser
    }
    // Data Manipulation
}

Java Code

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import java.net.URL;

public class FeedServlet extends HttpServlet {
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        try {
            response.setContentType("text/xml");
            final URL url;
            String line = "";

            if(request.getParameter("p").equals("w")) {
                // Configuration setting that returns: "http://xml.weather.yahoo.com/forecastrss?p=USOR0186"
                url = new URL(AppConfiguration.getInstance().getForcastUrl());
            } else {
                // Configuration setting that returns: "http://news.yahoo.com/rss/"
                url = new URL(AppConfiguration.getInstance().getNewsUrl());
            }
            final BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream());
            final PrintWriter writer = response.getWriter();

            while((line = reader.readLine()) != null) {
                writer.println(line);
                writer.flush();
            }
            writer.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

My company has a AppConfiguration class that allows for certain variables, like the URL's, to be changed through the configuration page. At any rate, those two calls simple return the urls...

Yahoo! Forcast RSS Feed:
http://xml.weather.yahoo.com/forecastrss?p=USOR0186

Yahoo! News: Top Stories Feed:
http://news.yahoo.com/rss/

Anyway, any help would be incredibly helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2024-12-04 04:38:17
for (i = 0; i < s.length; i++){

错误出现在小于号处,这意味着 XML 解析器正在读取您的源代码!使用 WGET 获取资源并检查是否返回实际的 XML,而不是源代码。

for (i = 0; i < s.length; i++){

The error is at the less-than symbol, which means that the XML parser is reading your source code! Use WGET to get the resource and check that actual XML is returned and not source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文