JSoup 解析 HTML
我正在尝试解析一个格式不正确的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您有一个
InputStream
而不是 URL?然后,您应该使用Jsoup#parse()
方法采用InputStream
:charsetName
应该是文档最初编码的字符集。您可以将其保留为 null,让 Jsoup 决定或回退到 UTF-8。baseUri
应该是最初提供 HTML 的 URL。您可以将其保留为null
,您将无法解析相对链接。但如果您确实拥有原始 URL,那么您也可以只使用
Jsoup#connect()
:无论您通过何种方式获取
Document
,您都可以使用 CSS 选择器 用于选择文档中感兴趣的元素。另请参阅有关该主题的 Jsoup 食谱。下面的示例从类名为personaltable
的第二列中提取所有数据:
结果如下:
So, you have an
InputStream
and not an URL? You should then use theJsoup#parse()
method which takes anInputStream
:The
charsetName
should be the charset the document is originally encoded in. You can leave itnull
to let Jsoup decide or fallback to UTF-8. ThebaseUri
should be the URL from which the HTML was originally served. You can leave itnull
, 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()
: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 ofpersonaltable
:which results in: