2012-06-04 61 views
0

我解析了一些RSS提要(嘗試不同的...),每次都非常隨機的字符被刪減。 我在做什麼錯?它爲什麼在某些情況下有效,而在其他情況下則不起作用? 還有另一種方法嗎? XML(大多數情況下)將包含UTF-8字符(如ä,ö,ü等),因此解決方案也應該與這些字符一起使用。解析一個RSS提要削減字符

如果您需要更多信息(更多代碼,更多詳細信息等),請讓我知道!

這裏是我的代碼:

public class RSSHandler extends DefaultHandler { 

final int state_unknown = 0; 
final int state_title = 1; 
final int state_description = 2; 
final int state_link = 3; 
final int state_pubdate = 4; 
int currentState = state_unknown; 
StringBuilder strCharacters; 

RSSFeed feed; 
RSSItem item; 

boolean inEntity = false; 
String entityName = ""; 

boolean itemFound = false; 

public RSSHandler() { 
    strCharacters = new StringBuilder(); 
} 

public RSSFeed getFeed() { 
    return feed; 
} 

@Override 
public void startDocument() throws SAXException { 
    feed = new RSSFeed(); 
    item = new RSSItem(); 
} 

@Override 
public void endDocument() throws SAXException { 
} 

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
    strCharacters = new StringBuilder(); 
    if (localName.equalsIgnoreCase("item")) { 
     itemFound = true; 
     item = new RSSItem(); 
     currentState = state_unknown; 
    } else if (localName.equalsIgnoreCase("title")) { 
     currentState = state_title; 
    } else if (localName.equalsIgnoreCase("description")) { 
     currentState = state_description; 
    } else if (localName.equalsIgnoreCase("link")) { 
     currentState = state_link; 
    } else if (localName.equalsIgnoreCase("pubdate")) { 
     currentState = state_pubdate; 
    } else { 
     currentState = state_unknown; 
    } 

} 

@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    if (itemFound == true) { 
     switch (currentState) { 
      case state_title: 
       item.setTitle(strCharacters.toString()); 
       break; 
      case state_description: 
       break; 
      case state_link: 
       item.setLink(strCharacters.toString()); 
       break; 
      case state_pubdate: 
       String dateStr = strCharacters.toString(); 
       SimpleDateFormat curFormater = new SimpleDateFormat(
         "EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH); 
       Date dateObj = null; 
       try { 
        dateObj = curFormater.parse(dateStr); 
        SimpleDateFormat postFormater = new SimpleDateFormat(
          "dd.MM.yyyy HH:mm"); 
        String newDateStr = postFormater.format(dateObj); 
        item.setPubdate(newDateStr); 
       } catch (ParseException e) { 
        e.printStackTrace(); 
       } 
       break; 
      default: 
       break; 
     } 
    } else { 
     switch (currentState) { 
      case state_title: 
       feed.setTitle(strCharacters.toString()); 
       break; 
      case state_description: 
       break; 
      case state_link: 
       feed.setLink(strCharacters.toString()); 
       break; 
      case state_pubdate: 
       feed.setPubdate(strCharacters.toString()); 
       break; 
      default: 
       break; 
     } 
    } 

    currentState = state_unknown; 

    if (localName.equalsIgnoreCase("item")) { 
     feed.addItem(item); 
    } 
} 

public void startEntity(String name) throws SAXException { 
    inEntity = true; 
    entityName = name; 
} 

@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 

    strCharacters = new StringBuilder(); 
    if (inEntity) { 
     inEntity = false; 
     strCharacters.append("&" + entityName + ";"); 
    } else { 
     for (int i = start; i < start + length; i++) { 
      strCharacters.append(ch[i]); 
     } 
    } 

    // strCharacters.append(ch, start, length); 
} 

}

+0

很難找到你做錯了什麼,但我可以建議你使用[XmlPullParser](http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html) –

+0

好,謝謝。 XmlPullParser有什麼好的教程嗎? – user754730

回答

0

你正在創建每個characters()調用一個新的StringBuilder。這是不正確的。有許多要求每個元素characters() - 你需要連接所有這些結果,而不是隻收集最後一塊。

+0

那麼我該怎麼做呢? – user754730

+1

@ user754730:在'startElement()'中創建'StringBuilder',在'characters()'中附加它,並在'endElement()'中使用結果。 – CommonsWare

+0

非常感謝!這完全是這樣做的(我有兩次新的StringBuilder(),而不是僅在startElement()中......) – user754730