2014-05-06 328 views
0

終止我試圖從任何新聞中顯示文本報紙的類別。我使用本報的RSS。但是,當我運行代碼時,有時候我會在上面得到異常消息,有時它會正常工作。這裏是我的RSS解析器代碼:XMLStreamException:[row,col]處的ParseError:[5,3]消息:元素類型「meta」必須以匹配結束標記「</meta>」

而且我使用RSS頁面:

RSSFeedParser parser = new RSSFeedParser("http://www.cumhuriyet.com.tr/rss/5"); 
Feed feed = parser.readFeed(); 

的RSS解析器代碼:

package main; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.xml.stream.XMLEventReader; 
import javax.xml.stream.XMLInputFactory; 
import javax.xml.stream.XMLStreamException; 
import javax.xml.stream.events.Characters; 
import javax.xml.stream.events.XMLEvent; 


public class RSSFeedParser { 
    static final String TITLE = "title"; 
    static final String DESCRIPTION = "description"; 
    static final String CHANNEL = "channel"; 
    static final String LANGUAGE = "language"; 
    static final String COPYRIGHT = "copyright"; 
    static final String LINK = "link"; 
    static final String AUTHOR = "author"; 
    static final String ITEM = "item"; 
    static final String PUB_DATE = "pubDate"; 
    static final String GUID = "guid"; 
    static final String IMG = "img"; 

    final URL url; 

    public RSSFeedParser(String feedUrl) { 
    try { 
     this.url = new URL(feedUrl); 
    } catch (MalformedURLException e) { 
     throw new RuntimeException(e); 
    } 
    } 

    public Feed readFeed() { 
    Feed feed = null; 
    try { 
     boolean isFeedHeader = true; 
     // Set header values intial to the empty string 
     String description = ""; 
     String title = ""; 
     String link = ""; 
     String language = ""; 
     String copyright = ""; 
     String author = ""; 
     String pubDate = ""; 
     String guid = ""; 
     String img = ""; 
     // First create a new XMLInputFactory 
     XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 
     // Setup a new eventReader 
     InputStream in = read(); 
     XMLEventReader eventReader = inputFactory.createXMLEventReader(in); 
     // read the XML document 
     while (eventReader.hasNext()) { 
     XMLEvent event = eventReader.nextEvent(); 
     if (event.isStartElement()) { 
      String localPart = event.asStartElement().getName() 
       .getLocalPart(); 
      switch (localPart) { 
      case ITEM: 
      if (isFeedHeader) { 
       isFeedHeader = false; 
       feed = new Feed(title, link, description, language, 
        copyright, pubDate); 
      } 
      event = eventReader.nextEvent(); 
      break; 
      case TITLE: 
      title = getCharacterData(event, eventReader); 
      break; 
      case DESCRIPTION: 
      description = getCharacterData(event, eventReader); 
      break; 
      case LINK: 
      link = getCharacterData(event, eventReader); 
      break; 
      case GUID: 
      guid = getCharacterData(event, eventReader); 
      break; 
      case LANGUAGE: 
      language = getCharacterData(event, eventReader); 
      break; 
      case AUTHOR: 
      author = getCharacterData(event, eventReader); 
      break; 
      case PUB_DATE: 
      pubDate = getCharacterData(event, eventReader); 
      break; 
      case COPYRIGHT: 
      copyright = getCharacterData(event, eventReader); 
      break; 
      } 
     } else if (event.isEndElement()) { 
      if (event.asEndElement().getName().getLocalPart() == (ITEM)) { 
      FeedMessage message = new FeedMessage(); 

      message.setDescription(description); 
      message.setPubDate(pubDate); 
      message.setLink(link); 
      message.setTitle(title); 
      message.setImg(img); 
      feed.getMessages().add(message); 
      event = eventReader.nextEvent(); 
      continue; 
      } 
     } 
     } 
    } catch (XMLStreamException e) { 
     throw new RuntimeException(e); 
    } 
    return feed; 
    } 

    private String getCharacterData(XMLEvent event, XMLEventReader eventReader) 
     throws XMLStreamException { 
    String result = ""; 
    event = eventReader.nextEvent(); 
    if (event instanceof Characters) { 
     result = event.asCharacters().getData(); 
    } 
    return result; 
    } 

    private InputStream read() { 
    try { 
     return url.openStream(); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
    } 
} 

異常消息是:

Exception in thread "main" java.lang.RuntimeException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[5,3] 
Message: The element type "meta" must be terminated by the matching end-tag "</meta>". 
    at main.RSSFeedParser.readFeed(RSSFeedParser.java:112) 
    at cumhuriyet.Dunya.cumDunya(Dunya.java:32) 
    at automation.ServerInteraction.main(ServerInteraction.java:83) 
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[5,3] 
Message: The element type "meta" must be terminated by the matching end-tag "</meta>". 
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source) 
    at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source) 
    at main.RSSFeedParser.readFeed(RSSFeedParser.java:58) 
    ... 2 more 

回答

0

這看起來像一個活文件;即相當頻繁改變的一個。它裏面也沒有標籤的標誌。

我能想到的兩種解釋正在發生的一切:

  1. 有時,正在產生或不正確地創建文檔。

  2. 有時您會得到一個HTML錯誤頁面而不是您期望的文檔 ,而XML解析器無法應對HTML中的標記 。

要跟蹤下來,你將不得不以捕獲導致解析失敗的精確輸入。

+0

我寫了上面的例外屏幕。 (我認爲你的意思是在你的消息結束:)) –

+0

做一件事..打印XML你得到,看到這個XML,每當你得到異常。我相信它不是你的問題,這是服務問題,有些時候發送錯誤的XML作爲迴應.. – niiraj874u

+0

你是對的,它是關於服務 –

相關問題