Java读取RSS:
然后是xpath的部分:
import java.io.IOException;
4
5 import javax.xml.parsers.*;
6 import javax.xml.xpath.*;
7
8 import org.w3c.dom.*;
9 import org.xml.sax.SAXException;
10
11 class XpathSample {
12 public static void main(String[] args) throws ParserConfigurationException,
13 SAXException, IOException, XPathExpressionException {
14 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
15 factory.setNamespaceAware(true);
16 DocumentBuilder builder = factory.newDocumentBuilder();
17 Document doc = builder.parse("XmlFile/books.xml");
18 XPathFactory xFactory = XPathFactory.newInstance();
19 XPath xpath = xFactory.newXPath();
20 XPathExpression expr = xpath
21 .compile("//book[author='Neal Stephenson']/price/text()");
22 Object result = expr.evaluate(doc, XPathConstants.NODESET);
23 NodeList nodes = (NodeList) result;
24 for (int i = 0; i < nodes.getLength(); i++) {
25 System.out.println(nodes.item(i).getNodeValue());
26 }
27 }
28
29 }
大概就是这样了。对啦对啦,还有yahoo的weather格式:
- //http://zfive.blogbus.com/logs/20959998.html
- package com.yourcompany.struts.user;
- import java.io.IOException;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- import java.net.URL;
- public class Test {
- public static void main(String[] args) {
- try{
- List result = new ArrayList(); //声明一个结果集
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //解析器工厂类
- DocumentBuilder db;
- Document doc;
- //此DocumentBuilder对象的作用是根据xml文件的url地址生成document对象
- db =dbf.newDocumentBuilder(); //得到一个DOM解析器对象;
- //File file = new File("students.xml"); //当然,我们也可以读出本地计算机中的xml文档
- //doc对象包含需要解析的feed的xml文件
- URL file = new URL("http://localhost:8080/rSS/pfocus.xml");
- doc = db.parse(file.openStream()); //把解析后的XML,赋给文档对象;
- //feed的xml文件的组成单位是item,从doc中取出所有的item
- NodeList nl = doc.getElementsByTagName("item"); //多个<item>标签组成一个链表;
- for(int i=0;i<nl.getLength();i++)
- {
- Element eltStu = (Element)nl.item(i);
- //此处得到元素中的<title></title>标签实体;
- Node titlenode = eltStu.getElementsByTagName("title").item(0);
- //返回第一个节点的值;
- String title = titlenode.getFirstChild().getNodeValue();
- Node linknode = eltStu.getElementsByTagName("link").item(0);
- String link = linknode.getFirstChild().getNodeValue();
- Node descriptionnode = eltStu.getElementsByTagName("description").item(0);
- String description = descriptionnode.getFirstChild().getNodeValue();
- Node authornode = eltStu.getElementsByTagName("author").item(0);
- String author = authornode.getFirstChild().getNodeValue();
- System.out.println(title);
- System.out.println(author);
- System.out.println(link);
- System.out.println(description);
- System.out.println("---------");
- }
- }
- catch(ParserConfigurationException e)
- {
- e.printStackTrace();
- }catch(SAXException e){
- e.printStackTrace();
- }catch(IOException e){
- e.printStackTrace();
- }finally{
- }
- }
- }
import java.io.IOException;
4
5 import javax.xml.parsers.*;
6 import javax.xml.xpath.*;
7
8 import org.w3c.dom.*;
9 import org.xml.sax.SAXException;
10
11 class XpathSample {
12 public static void main(String[] args) throws ParserConfigurationException,
13 SAXException, IOException, XPathExpressionException {
14 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
15 factory.setNamespaceAware(true);
16 DocumentBuilder builder = factory.newDocumentBuilder();
17 Document doc = builder.parse("XmlFile/books.xml");
18 XPathFactory xFactory = XPathFactory.newInstance();
19 XPath xpath = xFactory.newXPath();
20 XPathExpression expr = xpath
21 .compile("//book[author='Neal Stephenson']/price/text()");
22 Object result = expr.evaluate(doc, XPathConstants.NODESET);
23 NodeList nodes = (NodeList) result;
24 for (int i = 0; i < nodes.getLength(); i++) {
25 System.out.println(nodes.item(i).getNodeValue());
26 }
27 }
28
29 }
大概就是这样了。对啦对啦,还有yahoo的weather格式:
首先了解Yahoo Weather Api的RSS Response格式:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <rss version="2.0" xmlns:yweather="<a href="http://xml.weather.yahoo.com/ns/rss/1.0">http://xml.weather.yahoo.com/ns/rss/1.0</a>" xmlns:geo="<a href="http://www.w3.org/2003/01/geo/wgs84_pos">http://www.w3.org/2003/01/geo/wgs84_pos</a>#"> <channel> <title>Yahoo! Weather - Tangshan, CH</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html</link> <description>Yahoo! Weather for Tangshan, CH</description> <language>en-us</language> <lastBuildDate>Fri, 22 Aug 2008 8:00 am CST</lastBuildDate> <ttl>60</ttl> <yweather:location city="Tangshan" region="" country="CH"/> <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/> <yweather:wind chill="67" direction="290" speed="4" /> <yweather:atmosphere humidity="95" visibility="4.97" pressure="" rising="0" /> <yweather:astronomy sunrise="5:26 am" sunset="6:54 pm"/> <image> <title>Yahoo! Weather</title> <width>142</width> <height>18</height> <link>http://weather.yahoo.com</link> <url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url> </image> <item> <title>Conditions for Tangshan, CH at 8:00 am CST</title> <geo:lat>39.63</geo:lat> <geo:long>118.17</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html</link> <pubDate>Fri, 22 Aug 2008 8:00 am CST</pubDate> <yweather:condition text="Mostly Cloudy" code="28" temp="67" date="Fri, 22 Aug 2008 8:00 am CST" /> <description> <![CDATA[ <img src="<a href="http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif"/><br">http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif"/><br</a> /> <b>Current Conditions:</b><br /> Mostly Cloudy, 67 F<BR /> <BR /><b>Forecast:</b><BR /> Fri - Partly Cloudy. High: 86 Low: 69<br /> Sat - Partly Cloudy. High: 88 Low: 70<br /> <br /> <a href="<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html">Full">http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html">Full</a> Forecast at Yahoo! Weather</a><BR/> (provided by The Weather Channel)<br/> ]]> </description> <yweather:forecast day="Fri" date="22 Aug 2008" low="69" high="86" text="Partly Cloudy" code="30" /> <yweather:forecast day="Sat" date="23 Aug 2008" low="70" high="88" text="Partly Cloudy" code="30" /> <guid isPermaLink="false">CHXX0131_2008_08_22_8_00_CST</guid> </item> </channel> </rss>
我们所需要用到的Node是//rss/channel/item/yweather:forecast