2012-08-16 50 views
1

有看起來像這樣一些XML解析的文字: n(新行)刪除的Android

06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href="..."> ... <br>15:45 Something.. 

,並有很多的吧..

好了,我已經做到了這一點:

String mim =ses.replaceAll("(?s)\\<.*?\\>", " \n"); 

沒有其他方式可以很好地顯示文字。 現在,幾個看房,並在一段時間後,我需要的是相同的文字分成單獨的字符串是這樣的:

06:00 Vesti 

...或

07:15 Something Else 

我已經試過這樣的事情,但這是行不通的:

char[] rast = description.toCharArray(); 
    int brojac = 0; 
    for(int q=0; q<description.length(); q++){ 
     if(rast[q]=='\\' && rast[q+1]=='n') brojac++; 
    } 
    String[] niz = new String[brojac]; 

    int bf1=0; 
    int bf2=0; 
    int bf3=0; 
    int oo=0; 

    for(int q=0; q<description.length(); q++){ 
     if(rast[q]=='\\'&& rast[q+1]=='n'){ 
      bf3=bf1; 
      bf1=q; 

      String lol = description.substring(bf3, bf1); 
      niz[oo]=lol; 
      oo++; 
     } 
    } 

我知道,在description.substring(BF3,BF1)沒有被設置爲它們應該是,但我認爲這:

if(rast[q]=='\\' && rast[q+1]=='n) 

不能這樣工作..有沒有其他解決方案?

注意。沒有其他方法可以獲得該資源。 ,它必須通過這個。

+0

試過[分裂]的(http://stackoverflow.com/questions/3732790/android-split-string)? – 2012-08-16 13:25:11

+0

ty ..很少,但意味着很多..作品像一個魅力 – Igx33 2012-08-16 13:48:12

回答

1

調用Html.fromHtml(String)將正確地將<br>轉換爲\ n。

String html = "06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href=\"...\"> ... <br>15:45 Something.."; 
String str = Html.fromHtml(html).toString(); 
String[] arr = str.split("\n"); 

然後,只需把它分解一條線的基礎上 - 無需正則表達式(你不應該使用來分析在第一種情況下HTML)。

編輯:車削一切成一束Date小號

// Used to find the HH:mm, in case the input is wonky 
Pattern p = Pattern.compile("([0-2][0-9]:[0-5][0-9])"); 
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm"); 
SortedMap<Date, String> programs = new TreeMap<Date, String>(); 
for (String row : arr) { 
    Matcher m = p.matcher(row); 
    if (m.find()) { 
     // We found a time in this row 
     ParsePosition pp = new ParsePosition(m.start(0)); 
     Date when = fmt.parse(row, pp); 
     String title = row.substring(pp.getIndex()).trim(); 
     programs.put(when, title); 
    } 
} 
// Now programs contain the sorted list of programs. Unfortunately, since 
// SimpleDateFormat is stupid, they're all placed back in 1970 :-D. 
// This would give you an ordered printout of all programs *AFTER* 08:00 
Date filter = fmt.parse("08:00"); 
SortedMap<Date, String> after0800 = programs.tailMap(filter); 
// Since this is a SortedMap, after0800.values() will return the program names in order. 
// You can also iterate over each entry like so: 
for (Map.Entry<Date,String> program : after0800.entrySet()) { 
    // You can use the SimpleDateFormat to pretty-print the HH:mm again. 
    System.out.println("When:" + fmt.format(program.getKey())); 
    System.out.println("Title:" + program.getValue());    
} 
+0

沒有使用HTML.fromHTML(字符串),但我已經使用SPLIT,現在像一個魅力... ty – Igx33 2012-08-16 13:47:44

+0

Html.fromHtml(..)是可靠地刪除HTML從輸入(比正則表達式更多,因爲它使用實際的HTML解析器(通常是tagsoup)去除它)。 – Jens 2012-08-16 13:51:36

+0

你知道什麼,當我使用HTML.fromHtml(String)的東西...它的工作更好,然後才..花花公子,你是一個生活的救星..太多了! – Igx33 2012-08-16 13:53:44

0

使用正則表達式:

List<String> results = new ArrayList<String>(); 

Pattern pattern = Pattern.compile("(\d+:\d+ \w+)<?"); 
Matcher matcher = pattern.matcher("06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href="..."> ... <br>15:45 Something.."); 

while(matcher.find()) { 
    results.add(matcher.group(0)); 
} 

results最終將成爲一個字符串列表:

results = List[ 
    "06:00 Vesti", 
    "07:15 Something Else", 
    "09:10 Movie", 
    "15:45 Something.."] 

如何Java類的正則表達式庫的作品的想法見Rexgex Java Tutorial

+0

嗯,對不起,你的意思是這只是解析字符串或字符串,我已經叫String mim = ses.replaceAll(「(?s) \\ <.*?\\>「,」\ n「); ???我不太瞭解這些,但我只需要在該字符串mim上完成的操作,將所有HTML標記替換爲\ n。只需要擺脫\ n並將該文本設置爲數組或列表像你這樣的字符串... – Igx33 2012-08-16 13:31:50