解析rss問題的總結

Y2J
發布: 2017-04-24 16:35:55
原創
2478 人瀏覽過

try{ 
   org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
   org.dom4j.Document document = saxReader.read(rssUrl);
   org.dom4j.Element channel = (org.dom4j.Element) document.getRootElement().element("channel");
     int index =0;
           for (java.util.Iterator i = channel.elementIterator("item"); i.hasNext();){ 
            if(index+1>rssTable.Rows.length){
             break;
            } 
             org.dom4j.Element element = (org.dom4j.Element) i.next();
                String title = element.elementText("title");
                String link = element.elementText("link");
                String pubDate = element.elementText("pubDate");
                String source = element.elementText("source");  
                ci.adp.DataRow rssRow = rssTable.getDataRowByIndex(index);
              
                rssRow.setValue("TITLE", title);
                rssRow.setValue("LINK", link);
                rssRow.setValue("PUBDATE", pubDate);
                rssRow.setValue("SOURCE", source); 
                /*
                System.out.println("title标题: " + title);
                System.out.println("link链接: " + link);
                System.out.println("pubDate发布时间: " +pubDate); 
                System.out.println("source来源: " + source);
                */
                index++;  
           }
  }catch (Exception e) { 
   e.printStackTrace();
  }
登入後複製

 折騰了一上午,終於換個方式搞定了。

 上面這種dom4j方式,在我本機環境(windows+weblogic10+jdk1.6)下沒問題,
放到伺服器上(linux5.4 x64+weblogic10 +jdk1.6)上,總是報這個錯誤:

[oracle@ablw010215 bin]$ Warning: Caught exception attempting to use SAX to load a SAX XMLReader
Warning: Exception was: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.parsers.XIncludeAwareParserConfiguration
Warning: I will print the stack trace then carry on using the default SAX parser
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.parsers.XIncludeAwareParserConfiguration
        at org.xml.sax.helpers.XMLReaderFactory.loadClass(XMLReaderFactory.java:199)
        at org.xml.sax.helpers.XMLReaderFactory.createXMLReader(XMLReaderFactory.java:150)
        at org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:83)
        at org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)
        at org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)
        at org.dom4j.io.SAXReader.read(SAXReader.java:435)
        at org.dom4j.io.SAXReader.read(SAXReader.java:321)
        at jsp_servlet._ajax._comm._bbs.__iframearticlecontent._jspService(__iframearticlecontent.java:100)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:35)
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
        at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
        at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
        at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:27)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at ci.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:107)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
org.dom4j.DocumentException: SAX2 driver class weblogic.xml.jaxp.RegistryXMLReader does not implement XMLReader Nested exception: SAX2 driver class weblogic.xml.jaxp.RegistryXMLReader does not implement XMLReader
        at org.dom4j.io.SAXReader.read(SAXReader.java:484)
        at org.dom4j.io.SAXReader.read(SAXReader.java:321)
        at jsp_servlet._ajax._comm._bbs.__iframearticlecontent._jspService(__iframearticlecontent.java:99)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:35)
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
        at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
        at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
        at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:27)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at ci.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:107)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
Nested exception: 
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.parsers.XIncludeAwareParserConfiguration
        at org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:98)
        at org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)
        at org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)
        at org.dom4j.io.SAXReader.read(SAXReader.java:435)
        at org.dom4j.io.SAXReader.read(SAXReader.java:321)
        at jsp_servlet._ajax._comm._bbs.__iframearticlecontent._jspService(__iframearticlecontent.java:100)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:35)
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
        at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
        at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
        at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:27)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at ci.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:107)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
Nested exception: java.lang.ClassCastException: com.sun.org.apache.xerces.internal.parsers.XIncludeAwareParserConfiguration
        at org.dom4j.io.SAXHelper.createXMLReader(SAXHelper.java:98)
        at org.dom4j.io.SAXReader.createXMLReader(SAXReader.java:894)
        at org.dom4j.io.SAXReader.getXMLReader(SAXReader.java:715)
        at org.dom4j.io.SAXReader.read(SAXReader.java:435)
        at org.dom4j.io.SAXReader.read(SAXReader.java:321)
        at jsp_servlet._ajax._comm._bbs.__iframearticlecontent._jspService(__iframearticlecontent.java:100)
        at weblogic.servlet.jsp.JspBase.service(JspBase.java:35)
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
        at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
        at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
        at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:27)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at ci.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:107)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:57)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
登入後複製

<2012-11-8 上午10時56分08秒CST>
<2012-11-810時56分08CST> Socket>
web#pv .xml加上設定檔、加lib包等等都不起作用,換成這個就好了(依賴lib在我上傳的資源中,完全免費):

try { URL url = new URL(rss); // 读取Rss源 XmlReader reader = new XmlReader(url); System.out.println("Rss源的编码格式为:" + reader.getEncoding()); SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(reader); // 得到SyndFeed对象,即得到Rss源里的所有信息 List entries
 = feed.getEntries(); // 得到Rss新闻中子项列表 int index =0; for (int i = 0; i < entries.size(); i++) { // 循环得到每个子项信息 if(index+1>rssTable.Rows.length){ break; } SyndEntry entry = (SyndEntry) entries.get(i); /* System.out.println("标题:" + entry.getTitle()); System.out.println("连接地址:"
 + entry.getLink()); SyndContent description = entry.getDescription(); System.out.println("标题简介:" + description.getValue()); System.out.println("发布时间:" + entry.getPublishedDate()); // 以下是Rss源可先的几个部分 System.out.println("标题的作者:" + entry.getAuthor()); */ ci.adp.DataRow
 rssRow = rssTable.getDataRowByIndex(index); rssRow.setValue("TITLE", entry.getTitle()); rssRow.setValue("LINK", entry.getLink()); rssRow.setValue("PUBDATE", entry.getPublishedDate()); rssRow.setValue("SOURCE", entry.getAuthor()); /* // 此标题所属的范畴 List categoryList
 = entry.getCategories(); if (categoryList != null) { for (int m = 0; m < categoryList.size(); m++) { SyndCategory category = (SyndCategory) categoryList .get(m); System.out.println("此标题所属的范畴:" + category.getName()); } } */ index++; /* // 得到流媒体播放文件的信息列表 List
 enclosureList = entry.getEnclosures(); if (enclosureList != null) { for (int n = 0; n < enclosureList.size(); n++) { SyndEnclosure enclosure = (SyndEnclosure) enclosureList .get(n); System.out.println("流媒体播放文件:" + entry.getEnclosures()); } } */ } } catch (Exception
 e) { e.printStackTrace(); }
登入後複製

 

org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();org.dom4j.Document document = saxReader.read(rssUrl);org.dom4j.Element channel = (org.dom4j.Element) document.getRootElement().element("channel"); int index =0; for (java.util.Iterator i = channel.elementIterator("item");
 i.hasNext();){ if(index+1>rssTable.Rows.length){ break; } org.dom4j.Element element = (org.dom4j.Element) i.next(); String title = element.elementText("title"); String link = element.elementText("link"); String pubDate = element.elementText("pubDate"); String
 source = element.elementText("source"); ci.adp.DataRow rssRow = rssTable.getDataRowByIndex(index); rssRow.setValue("TITLE", title); rssRow.setValue("LINK", link); rssRow.setValue("PUBDATE", pubDate); rssRow.setValue("SOURCE", source); /* System.out.println("title标题:
 " + title); System.out.println("link链接: " + link); System.out.println("pubDate发布时间: " +pubDate); System.out.println("source来源: " + source); */ index++; }
登入後複製

以上是解析rss問題的總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
rss
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!