Jsoup解析HTML的问题 [英] Jsoup parsing HTML issue
本文介绍了Jsoup解析HTML的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来Jsoup,我试图分析一个网站,用下面的HTML和检索的HTML文本输入的值,特别是值= 14然后我要显示该值(因为在我的Android应用程序文本视图的字符串在这种情况下,14号)。我已经尝试了多种方法,但它没有工作,我刚收到空。请出示的例子。
< DIV ID =PatientsCurrentlyInClinic的风格=显示:无> < - 被点击这些属性的链接时,显示消息:HREF =#信息的rel =模态 - > < H3>该挂哪个科,你更新< / H3 GT&;
<形式的行动=方法=获取>
&所述p为H.;
<选择名称=patientclinicidID =patientclinicid><期权价值=2选择>选址的两< /选项><期权价值=1>位置的One< /选项><选项值=3>望三< /选项>< /选择> &所述; / P> < H4>有多少病人在门诊<?/ H4>
&所述p为H.;
对您的患者提供更好的服务,请您进入诊所的病人当前数目。
&所述; / P>
<输入级=文字输入中输入类型=文本ID =小投入NAME =patientsInClinicVALUE =14/> < P><输入类=按钮NAME =患者诊所类型=提交值=更新/>< / P>
< /表及GT; < / DIV> &所述;! - 结束#messages - >
我的尝试,让我空如下:
私有类标题扩展的AsyncTask<太虚,太虚,太虚> {
字符串名称;
字符串值;
@覆盖
在preExecute保护无效(){
super.on preExecute();
mProgressDialog =新ProgressDialog(HTML.this);
mProgressDialog.setTitle(检查数据库);
mProgressDialog.setMessage(正在加载...);
mProgressDialog.setIndeterminate(假);
mProgressDialog.show();
} @覆盖
保护无效doInBackground(虚空...... PARAMS){
尝试{ 文档的DOC = Jsoup.connect(URL)获得();
元素inputElems = doc.select(输入#小投入);
对于(元inputElem:inputElems){
名称= inputElem.attr(名称);
值= inputElem.attr(值);
}
}赶上(的Throwable t)的{
t.printStackTrace();
}
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
//设置标题为的TextView
TextView的txttitle =(的TextView)findViewById(R.id.showPatientNumber);
txttitle.setText(值);
mProgressDialog.dismiss();
}
}
解决方案
试试这个,
元素inputElems = doc.select(输入);
迭代&所述;组件> linksIt = inputElems .iterator();而(linksIt.hasNext()){
元件inputElem = linksIt.next();
字符串ID = inputElem.attr(ID); 如果(id.equals(小投入)){
名称= inputElem.attr(名称);
值= inputElem.attr(值);
}
}
I am new to Jsoup and am trying to parse a website, with the following html, and retrieve the value of the text input in the html below, specifically the "value=14" which I then want to display that value (the number 14 in this case) as a string in a text view in my android app. I have tried multiple ways but it hasn't worked, I just receive "null". Please show example.
<div id="PatientsCurrentlyInClinic" style="display: none"> <!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal" -->
<h3>Which clinic are you updating?</h3>
<form action="" method="get">
<p>
<select name="patientclinicid" id="patientclinicid"><option value="2" selected>Location Two</option><option value="1">Location One</option><option value="3">Location Three</option></select> </p>
<h4>How many patients are in the clinic?</h4>
<p>
To provide better service to your patients, please enter the current number of patients in your clinic.
</p>
<input class="text-input medium-input" type="text" id="small-input" name="patientsInClinic" value="14"/>
<p><input class="button" name="patients-clinic" type="submit" value="Update" /></p>
</form>
</div> <!-- End #messages -->
My attempt that gives me "null" is as follows:
private class Title extends AsyncTask<Void, Void, Void> {
String name;
String value;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(HTML.this);
mProgressDialog.setTitle("Checking Database");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect(url).get();
Elements inputElems =doc.select("input#small-input");
for (Element inputElem : inputElems){
name = inputElem.attr("name");
value = inputElem.attr("value");
}
} catch(Throwable t) {
t.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.showPatientNumber);
txttitle.setText(value);
mProgressDialog.dismiss();
}
}
解决方案
try this,
Elements inputElems =doc.select("input");
Iterator<Element> linksIt = inputElems .iterator();
while (linksIt.hasNext()) {
Element inputElem = linksIt.next();
String id = inputElem.attr("id");
if(id.equals("small-input")){
name = inputElem.attr("name");
value= inputElem.attr("value");
}
}
这篇关于Jsoup解析HTML的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文