博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 读取 RSS,顺便还有xpath的用法
阅读量:4120 次
发布时间:2019-05-25

本文共 4933 字,大约阅读时间需要 16 分钟。

Java读取RSS:
  1. //http://zfive.blogbus.com/logs/20959998.html
  2. package com.yourcompany.struts.user;
  3. import java.io.IOException;
  4. import java.io.File;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.Node;
  13. import org.w3c.dom.NodeList;
  14. import org.xml.sax.SAXException;
  15. import java.net.URL;
  16. public class Test {
  17.  public static void main(String[] args) {
  18.   try{
  19.    List result = new ArrayList();  //声明一个结果集
  20.    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //解析器工厂类
  21.    DocumentBuilder db;
  22.    Document doc;
  23.   
  24.    //此DocumentBuilder对象的作用是根据xml文件的url地址生成document对象
  25.    db =dbf.newDocumentBuilder();     //得到一个DOM解析器对象;
  26.     //File file = new File("students.xml"); //当然,我们也可以读出本地计算机中的xml文档
  27.   
  28.    //doc对象包含需要解析的feed的xml文件
  29.    URL file = new URL("http://localhost:8080/rSS/pfocus.xml");
  30.    
  31.    doc = db.parse(file.openStream());  //把解析后的XML,赋给文档对象; 
  32.    //feed的xml文件的组成单位是item,从doc中取出所有的item
  33.    NodeList nl = doc.getElementsByTagName("item");  //多个<item>标签组成一个链表;
  34.  
  35.    for(int i=0;i<nl.getLength();i++)
  36.    {
  37.     Element eltStu = (Element)nl.item(i);
  38.     //此处得到元素中的<title></title>标签实体;
  39.     Node titlenode = eltStu.getElementsByTagName("title").item(0);  
  40.     //返回第一个节点的值;
  41.       String title = titlenode.getFirstChild().getNodeValue();
  42.     
  43.     Node linknode = eltStu.getElementsByTagName("link").item(0);
  44.     String link = linknode.getFirstChild().getNodeValue();
  45.     
  46.     Node descriptionnode = eltStu.getElementsByTagName("description").item(0);
  47.     String description = descriptionnode.getFirstChild().getNodeValue();
  48.     
  49.     Node authornode = eltStu.getElementsByTagName("author").item(0);
  50.     String author = authornode.getFirstChild().getNodeValue();
  51.     
  52.     
  53.     System.out.println(title);
  54.     System.out.println(author);
  55.     System.out.println(link);
  56.     System.out.println(description);
  57.     System.out.println("---------");
  58.       }
  59.   }
  60.    catch(ParserConfigurationException e)
  61.    {
  62.     e.printStackTrace();
  63.    }catch(SAXException e){
  64.     e.printStackTrace();
  65.    }catch(IOException e){
  66.     e.printStackTrace();
  67.    }finally{
  68.    }
  69. }
  70. }
然后是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格式:

首先了解Yahoo Weather Api的RSS Response格式:

http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="
http://www.w3.org/2003/01/geo/wgs84_pos#"> 
   
Yahoo! Weather - Tangshan, CH   
http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html   
Yahoo! Weather for Tangshan, CH
   
en-us
   
Fri, 22 Aug 2008 8:00 am CST
   
60
   
   
   
   
   
   
     
Yahoo! Weather     
142
     
18
     
http://weather.yahoo.com     
http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif
       
     
Conditions for Tangshan, CH at 8:00 am CST     
39.63
     
118.17
     
http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html     
Fri, 22 Aug 2008 8:00 am CST
     
     
        http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif"/> />Current Conditions:
Mostly Cloudy, 67 F

Forecast:
Fri - Partly Cloudy. High: 86 Low: 69
Sat - Partly Cloudy. High: 88 Low: 70

Full">http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html">Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>     
     
     
     
CHXX0131_2008_08_22_8_00_CST
   
 

我们所需要用到的Node是//rss/channel/item/yweather:forecast

转载地址:http://zwipi.baihongyu.com/

你可能感兴趣的文章
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>
《达芬奇的人生密码》观后感
查看>>
论文翻译:《一个包容性设计的具体例子:聋人导向可访问性》
查看>>
基于“分形”编写的交互应用
查看>>
《融入动画技术的交互应用》主题博文推荐
查看>>
链睿和家乐福合作推出下一代零售业隐私保护技术
查看>>
Unifrax宣布新建SiFAB™生产线
查看>>
艾默生纪念谷轮™在空调和制冷领域的百年创新成就
查看>>
NEXO代币持有者获得20,428,359.89美元股息
查看>>
Piper Sandler为EverArc收购Perimeter Solutions提供咨询服务
查看>>
RMRK筹集600万美元,用于在Polkadot上建立先进的NFT系统标准
查看>>
JavaSE_day12 集合
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
Day_15JavaSE 异常
查看>>
异常 Java学习Day_15
查看>>
JavaSE_day_03 方法
查看>>