从HTML解析信息以找出选定的选项 [英] Parsing information from HTML to find out the selected option

查看:64
本文介绍了从HTML解析信息以找出选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java和Jsoup库从html解析文本. 在html中,我有两个选项,称为Single和Multiple.两者旁边都有一个复选标记,并且如果选中的选项是"Single",则选中该复选框,而"Multiple"未选中.我需要找到一种使用代码的方法,选择该选项是多个选项还是单个选项.非常感谢您的帮助.谢谢.

I am using Java and Jsoup library to parse the text out of html. In the html I have two options called Single and Multiple. Both have a check mark next to them and if the option selected is Single the checkbox is check and the box the Multiple is not checked. I need to find a way using code which option is selected whether it's multiple or single. I truly appreciate your help. Thank you.


  <table class="MsoNormalTable" style="border-collapse:collapse;mso-yfti-tbllook:1184;mso-padding-alt:0in 0in 0in 0in" cellspacing="0" cellpadding="0" border="1">
   <tbody><tr style="mso-yfti-irow:0;mso-yfti-firstrow:yes;height:21.0pt">
    <td style="width:36.75pt;border-top:solid windowtext 1.0pt;
    border-left:solid windowtext 1.0pt;border-bottom:none;border-right:none;
    background:#F2F2F2;padding:.75pt .75pt .75pt .75pt;height:21.0pt" width="49">
    <p class="MsoNormal" style="margin-top:0in;margin-right:-6.0pt;
    margin-bottom:0in;margin-left:-6.0pt;margin-bottom:.0001pt;text-align:center;
    vertical-align:baseline" align="center">​​<span style="font-family:&quot;MS Gothic&quot;">☒</span>​&nbsp;&nbsp;</p>
    </td>
    <td style="width:442.5pt;border-top:solid windowtext 1.0pt;
    border-left:none;border-bottom:none;border-right:solid windowtext 1.0pt;
    background:#F2F2F2;padding:.75pt .75pt .75pt .75pt;height:21.0pt" width="590">
    <p class="MsoNormal" style="margin-left:1.5pt;vertical-align:baseline"><span style="font-size:10.0pt">Single&nbsp;</span></p>
    </td>
   </tr>
   <tr style="mso-yfti-irow:1;mso-yfti-lastrow:yes">
    <td style="width:36.75pt;border:none;border-left:solid windowtext 1.0pt;
    background:#F2F2F2;padding:.75pt .75pt .75pt .75pt" width="49">
    <p class="MsoNormal" style="margin-top:0in;margin-right:-6.0pt;
    margin-bottom:0in;margin-left:-6.0pt;margin-bottom:.0001pt;text-align:center;
    vertical-align:baseline" align="center">​​<span style="font-family:&quot;MS Gothic&quot;">☐</span>​&nbsp;</p>
    </td>
    <td style="width:442.5pt;border:none;border-right:solid windowtext 1.0pt;
    background:#F2F2F2;padding:.75pt .75pt .75pt .75pt" width="590">
    <p class="MsoNormal" style="margin-left:1.5pt;vertical-align:baseline"><span style="font-size:10.0pt">Multiple&nbsp;</span></p>
    </td>
   </tr>
  </tbody></table>
  <p class="MsoNormal" style="vertical-align:baseline"><span style="font-size:
  10.0pt;font-family:&quot;Times New Roman&quot;,serif">&nbsp;</span></p>

推荐答案

尝试一下:

Document doc = Jsoup.parse(html);
String selected = doc.select("tr:contains(☒) td:eq(1)").first().text();

selected然后应包含所选选项"Single""Multiple"的文本.

selected should then contain the text of the selected option, "Single" or "Multiple".

tr:contains(☒)选择器选择包含☒符号的表行,然后td:eq(1)选择该行中的第二个表单元格. (它从0开始计数,所以第一个单元格是数字0,第二个单元格是数字1,依此类推.)

The tr:contains(☒) selector picks the table row that contains the ☒ symbol, and td:eq(1) then selects the second table cell in that row. (It counts starting at 0, so the first cell is number 0, the second cell is number 1 etc.)

这篇关于从HTML解析信息以找出选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆