java - 如何使用java读取某个URL的xml文件?

标签 java xml url web

我想读取给定 URL 的内容,但采用 XML 格式而不是 HTML 格式。

示例 RSS 响应如下所示: 我想做的就是检索这样的样本而不是 html 数据

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
  <title>Yahoo! Weather - Sunnyvale, CA</title>
  <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
  <description>Yahoo! Weather for Sunnyvale, CA</description>
  <language>en-us</language>
  <lastBuildDate>Fri, 18 Dec 2009 9:38 am PST</lastBuildDate>
  <ttl>60</ttl>
  <yweather:location city="Sunnyvale" region="CA"   country="United States"/>
  <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
  <yweather:wind chill="50"   direction="0"   speed="0" />
  <yweather:atmosphere humidity="94"  visibility="3"  pressure="30.27"  rising="1" />
  <yweather:astronomy sunrise="7:17 am"   sunset="4:52 pm"/>
  <image>
    <title>Yahoo! Weather</title>
    <width>142</width>
    <height>18</height>
    <link>http://weather.yahoo.com</link>
    <url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
  </image>
  <item>
    <title>Conditions for Sunnyvale, CA at 9:38 am PST</title>
    <geo:lat>37.37</geo:lat>
    <geo:long>-122.04</geo:long>
    <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
    <pubDate>Fri, 18 Dec 2009 9:38 am PST</pubDate>
    <yweather:condition  text="Mostly Cloudy"  code="28"  temp="50"  date="Fri, 18 Dec 2009 9:38 am PST" />
    <description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 50 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 62 Low: 49<br />
Sat - Partly Cloudy. High: 65 Low: 49<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
    <yweather:forecast day="Fri" date="18 Dec 2009" low="49" high="62" text="Partly Cloudy" code="30" />
    <yweather:forecast day="Sat" date="19 Dec 2009" low="49" high="65" text="Partly Cloudy" code="30" />
    <guid isPermaLink="false">USCA1116_2009_12_18_9_38_PST</guid>
  </item>
</channel>
</rss>

我使用了这段代码,但出现错误:

package search;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;


public class Process{
    public static void main ( String [] args ) throws IOException{

        URL xmlUrl = new URL("http://www.yahoo.com");
        InputStream in = xmlUrl.openStream();
        Document doc = parse(in);

    }

    public static Document parse (InputStream is) {
        Document ret = null;
        DocumentBuilderFactory domFactory;
        DocumentBuilder builder;

        try {
            domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setValidating(false);
            domFactory.setNamespaceAware(false);
            builder = domFactory.newDocumentBuilder();

            ret = builder.parse(is);
        }
        catch (Exception ex) {
            System.err.println("unable to load XML: " + ex);
        }
        return ret;
    }
}

错误:

[Fatal Error] :7:17: The entity "lrm" was referenced, but not declared.
unable to load XML: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 17; The entity "lrm" was referenced, but not declared.

最佳答案

向您的 URL 发出 HTTP 请求并解析返回的字符串。我错过重点了吗?我没明白你的问题...

关于java - 如何使用java读取某个URL的xml文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16565233/

相关文章:

url - 从 URL 中删除应用程序名称

java - Web 服务服务器问题

xml - 当使用 msxml 的 XSLT 转换与根节点 ('/' 不匹配时该怎么办?

Android 应用程序快捷方式不起作用

excel - 在 Microsoft EXCEL 中将 URL 格式的内容转换为纯文本

objective-c - 使用 NSAppleEventManager 和 kInternetEventClass/kAEGetURL 的 Cocoa 协议(protocol)处理程序

Java ByteArray 文件解析

java - 空字符串上的通配符

java - OnClickListener 未检测到点击。类未加载

java - 在滑动选项卡布局中停留在同一选项卡中时切换到不同的 fragment