JSoup 解析 HTML

发布于 2024-12-10 15:28:37 字数 4630 浏览 0 评论 0原文

我正在尝试解析一个格式不正确的 DTD html 文件,我使用 JSOUP 通过输入流检索该文件,并获取 TD 字段中的所有数据。 我怎样才能用 JSoup 做到这一点? 我已经看过 http://jsoup.org/cookbook/ 但我应该需要一些例子来获取它开始了。

先感谢您。

我已经尝试过 saxparser 但无法让 DTD 工作。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-             strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> 
<TABLE class=personaltable cellSpacing=0 cellPadding=0> 
 <TBODY> 
  <TR class=alternativerow> 
   <TD>Nieuw beltegoed:</TD> 
   <TD>€ 1,00</TD></TR> 
  <TR> 
   <TD>Tegoed vorige periode:  
   <TD>€ 2,00</TD></TD></TR> 
  <TR class=alternativerow> 
   <TD>Tegoed tot 09-11-2011:  
   <TD>€ 10,00</TD></TD></TR> 
  <TR> 
   <TD> 
   <TD height=25></TD> 
  <TR class=alternativerow> 
   <TD>Verbruik sinds nieuw tegoed:</TD> 
   <TD>€ 0,33</TD></TR> 
  <TR> 
   <TD>Ongebruikt tegoed:</TD> 
   <TD>€ 12,00</TD></TR> 
  <TR class=alternativerow> 
   <TD class=f-Orange>Verbruik boven bundel:</TD> 
   <TD class=f-Orange>€ 0,00</TD></TR> 
  <TR> 
   <TD>Verbruik dat niet in de bundel zit*:</TD> 
   <TD>€ 0,00</TD></TR> 
  </TBODY> 
 </TABLE> 
</html> 

编辑: 我正在强制关闭,我需要 AsyncTask 中的 JSoup。 这是 LOgcat:

10-20 21:07:36.679: ERROR/AndroidRuntime(1396): FATAL EXCEPTION: main
10-20 21:07:36.679: ERROR/AndroidRuntime(1396): java.lang.NullPointerException
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at   com.sencide.AndroidLogin$MyTask.onPostExecute(AndroidLogin.java:276)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at com.sencide.AndroidLogin$MyTask.onPostExecute(AndroidLogin.java:1)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.AsyncTask.finish(AsyncTask.java:417)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.Looper.loop(Looper.java:130)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.app.ActivityThread.main(ActivityThread.java:3835)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at java.lang.reflect.Method.invoke(Method.java:507)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at dalvik.system.NativeStart.main(Native Method)

这是 AsyncTask 代码:

public class MyTask extends AsyncTask<String, Integer, String> {
    private Elements tdsFromSecondColumn=null;
}

protected String doInBackground(String... params) {
      InputStream inputStreamActivity = response.getEntity().getContent();

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreamActivity));
                StringBuilder sb = new StringBuilder();
                String line = null;

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                /******* CLOSE CONNECTION AND STREAM *******/

                System.out.println(sb);
                inputStreamActivity.close();

                String kpn;
                kpn = sb.toString();

                Document doc = Jsoup.parse(kpn);
                Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(1)");
}

@Override 
    protected void onPostExecute(String result) { 
        //publishProgress(false); 
        TextView tv = (TextView)findViewById(R.id.lbl_top);

        for (Element tdFromSecondColumn : tdsFromSecondColumn) { 
            //System.out.println(tdFromSecondColumn.text()); 
            tv.setText("");
            tv.setText(tdFromSecondColumn.text());
        }
}
}

I am trying to parse a non well formed DTD html file which i retrieve by a inputstream with JSOUP, and get all the data in the TD fields.
How can i do that with JSoup?
I already looked at the http://jsoup.org/cookbook/ but i should need som example to get it started.

Thank you in advance.

I already tried the saxparser but i can`t get the DTD to work.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-             strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> 
<TABLE class=personaltable cellSpacing=0 cellPadding=0> 
 <TBODY> 
  <TR class=alternativerow> 
   <TD>Nieuw beltegoed:</TD> 
   <TD>€ 1,00</TD></TR> 
  <TR> 
   <TD>Tegoed vorige periode:  
   <TD>€ 2,00</TD></TD></TR> 
  <TR class=alternativerow> 
   <TD>Tegoed tot 09-11-2011:  
   <TD>€ 10,00</TD></TD></TR> 
  <TR> 
   <TD> 
   <TD height=25></TD> 
  <TR class=alternativerow> 
   <TD>Verbruik sinds nieuw tegoed:</TD> 
   <TD>€ 0,33</TD></TR> 
  <TR> 
   <TD>Ongebruikt tegoed:</TD> 
   <TD>€ 12,00</TD></TR> 
  <TR class=alternativerow> 
   <TD class=f-Orange>Verbruik boven bundel:</TD> 
   <TD class=f-Orange>€ 0,00</TD></TR> 
  <TR> 
   <TD>Verbruik dat niet in de bundel zit*:</TD> 
   <TD>€ 0,00</TD></TR> 
  </TBODY> 
 </TABLE> 
</html> 

Edit:
I am getting a force close, i need the JSoup in my AsyncTask.
Here is the LOgcat:

10-20 21:07:36.679: ERROR/AndroidRuntime(1396): FATAL EXCEPTION: main
10-20 21:07:36.679: ERROR/AndroidRuntime(1396): java.lang.NullPointerException
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at   com.sencide.AndroidLogin$MyTask.onPostExecute(AndroidLogin.java:276)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at com.sencide.AndroidLogin$MyTask.onPostExecute(AndroidLogin.java:1)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.AsyncTask.finish(AsyncTask.java:417)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.os.Looper.loop(Looper.java:130)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at android.app.ActivityThread.main(ActivityThread.java:3835)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at java.lang.reflect.Method.invoke(Method.java:507)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-20 21:07:36.679: ERROR/AndroidRuntime(1396):     at dalvik.system.NativeStart.main(Native Method)

Here is the AsyncTask code:

public class MyTask extends AsyncTask<String, Integer, String> {
    private Elements tdsFromSecondColumn=null;
}

protected String doInBackground(String... params) {
      InputStream inputStreamActivity = response.getEntity().getContent();

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreamActivity));
                StringBuilder sb = new StringBuilder();
                String line = null;

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                /******* CLOSE CONNECTION AND STREAM *******/

                System.out.println(sb);
                inputStreamActivity.close();

                String kpn;
                kpn = sb.toString();

                Document doc = Jsoup.parse(kpn);
                Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(1)");
}

@Override 
    protected void onPostExecute(String result) { 
        //publishProgress(false); 
        TextView tv = (TextView)findViewById(R.id.lbl_top);

        for (Element tdFromSecondColumn : tdsFromSecondColumn) { 
            //System.out.println(tdFromSecondColumn.text()); 
            tv.setText("");
            tv.setText(tdFromSecondColumn.text());
        }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-12-17 15:28:37

那么,您有一个 InputStream 而不是 URL?然后,您应该使用 Jsoup#parse() 方法采用 InputStream

Document document = Jsoup.parse(inputStream, charsetName, baseUri);
// ...

charsetName 应该是文档最初编码的字符集。您可以将其保留为 null,让 Jsoup 决定或回退到 UTF-8。 baseUri 应该是最初提供 HTML 的 URL。您可以将其保留为 null,您将无法解析相对链接。

但如果您确实拥有原始 URL,那么您也可以只使用 Jsoup#connect()

Document document = Jsoup.connect(url).get();
// ...

无论您通过何种方式获取 Document,您都可以使用 CSS 选择器 用于选择文档中感兴趣的元素。另请参阅有关该主题的 Jsoup 食谱。下面的示例从类名为 personaltable第二列中提取所有数据:

Elements tdsFromSecondColumn = document.select("table.personaltable td:eq(1)");

for (Element tdFromSecondColumn : tdsFromSecondColumn) {
    System.out.println(tdFromSecondColumn.text());
}

结果如下:

€ 1,00
€ 2,00
€ 10,00

€ 0,33
€ 12,00
€ 0,00
€ 0,00

So, you have an InputStream and not an URL? You should then use the Jsoup#parse() method which takes an InputStream:

Document document = Jsoup.parse(inputStream, charsetName, baseUri);
// ...

The charsetName should be the charset the document is originally encoded in. You can leave it null to let Jsoup decide or fallback to UTF-8. The baseUri should be the URL from which the HTML was originally served. You can leave it null, you'll only not be able to resolve relative links.

But if you actually have the original URL, then you could also just use Jsoup#connect():

Document document = Jsoup.connect(url).get();
// ...

Regardless of the way you obtained the Document, you can use CSS selectors to select elements of interest in the document. See also the Jsoup cookbook on that subject. Here's an example which extracts all the data from the 2nd column of the <table> with a class name of personaltable:

Elements tdsFromSecondColumn = document.select("table.personaltable td:eq(1)");

for (Element tdFromSecondColumn : tdsFromSecondColumn) {
    System.out.println(tdFromSecondColumn.text());
}

which results in:

€ 1,00
€ 2,00
€ 10,00

€ 0,33
€ 12,00
€ 0,00
€ 0,00
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文