2015-11-18 87 views
0

我正在開發一個Android應用程序,我必須顯示每日聖訓。我有一個每天更新聖訓的鏈接。這是該頁面的後端html。我怎樣才能從這個HTML使用jsoup提取數據

<div class="hadith-explanation" id="hadithcontent"> 
<h2>Today's Hadith</h2> 
<br> 
<h3>Commitments</h3> 
<br> 
<p>The Messenger of Allah (sal Allahu alaihi wa sallam) said: "He has (really) no faith who fulfills not his trust, 
    and he has (really) no religion who fulfills not his promise." [Baihaqi]<br /><br />Always keep your word. 
    Each time you keep a commitment you are rewarded by Allah (subhana wa ta'ala) for obeying Him. If you mix a few drops of wine in a glass 
    full of water, it spoils the whole glass of water and makes it unfit for consumption. Similarly, dishonesty in any sphere of your life 
    permeates and corrupts your entire nature and eeman. When a person&rsquo;s words carry no weight, it only reveals his/her treacherous nature. 
    <br /><br />If you promise to be somewhere, make sure you are there on time. If you promise to call somebody back, do so on time. Don't commit 
    what you cannot do. One minute means 60 seconds everywhere, no more. Make a habit of under-committing rather than over-committing.<br /><br /> 
    The online version of Daily Hadith is available. Please visit http://dailyhadith.adaptivesolutionsinc.com 
</p> 

我想從段標記開始直到第一個br標記。即從'安拉的信使'開始,直到[白哈奇]。

我已經在網上搜索,發現它可以使用jsoup庫來完成,但我沒有太多關於它的編程知識。有人請指導我。

回答

0

您可以使用childNodes()來完成。

的Java:

package com.github.davidepastore.stackoverflow33780189; 

import java.io.IOException; 
import java.io.InputStream; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 

/** 
* Stackoverflow 33780189 
* 
*/ 
public class App 
{ 
    public static void main(String[] args) throws IOException 
    { 
     ClassLoader classloader = Thread.currentThread() 
       .getContextClassLoader(); 
     InputStream is = classloader.getResourceAsStream("file.html"); 
     Document document = Jsoup.parse(is, "UTF-8", ""); 
     Element element = document.select(".hadith-explanation p").first(); 
     String result = element.childNodes().get(0).toString(); 

     System.out.println("Result: " + result); 
    } 
} 

file.html:

<div class="hadith-explanation" id="hadithcontent"> 
    <h2>Today's Hadith</h2> 
    <br> 
    <h3>Commitments</h3> 
    <br> 
    <p> 
     The Messenger of Allah (sal Allahu alaihi wa sallam) said: "He has 
     (really) no faith who fulfills not his trust, and he has (really) no 
     religion who fulfills not his promise." [Baihaqi]<br /> 
     <br />Always keep your word. Each time you keep a commitment you are 
     rewarded by Allah (subhana wa ta'ala) for obeying Him. If you mix a 
     few drops of wine in a glass full of water, it spoils the whole glass 
     of water and makes it unfit for consumption. Similarly, dishonesty in 
     any sphere of your life permeates and corrupts your entire nature and 
     eeman. When a person&rsquo;s words carry no weight, it only reveals 
     his/her treacherous nature. <br /> 
     <br />If you promise to be somewhere, make sure you are there on 
     time. If you promise to call somebody back, do so on time. Don't 
     commit what you cannot do. One minute means 60 seconds everywhere, no 
     more. Make a habit of under-committing rather than over-committing.<br /> 
     <br /> The online version of Daily Hadith is available. Please visit 
     http://dailyhadith.adaptivesolutionsinc.com 
    </p> 
</div> 

A similar case here on stackoverflow