所以我正在学习如何使用 XPath 和 HtmlCleaner 来解析 HTML,但我遇到了一个问题。这是代码:
public class ScheudeleWithDesign extends Activity {
static final String urlToParse = "https://www.easistent.com/urniki/263/razredi/18221";
static final String xpathTableContents = "//div[@id='text11']";
TextView tw1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scheudele_with_design);
tw1 = (TextView) findViewById(R.id.urnikText);
String value = "";
value = new getScheudele().execute().toString();
tw1.setText(value);
}//End of onCreate
private class getScheudele extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String stats = null;
//cleaner properties
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(false);
props.setAllowMultiWordAttributes(false);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
URL url;
try {
url = new URL(urlToParse);
TagNode root = cleaner.clean(url);
Object[] node = root.evaluateXPath(xpathTableContents);
//Vzemi podatke če najdeš element
if (node.length > 0) {
TagNode resultNode = (TagNode)node[10];
stats = resultNode.getText().toString();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPatherException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stats;
}
}
所以我显然是在尝试解析某些数据并将其设置为 textview。这不起作用,结果完全错误:
com.whizzapps.stpsurniki.ScheudeleWithDesign$getScheudele@421a7d90
我的猜测是问题出在 XPath 中:
static final String xpathTableContents = "//div[@id='text11']";
我以前从未使用过 XPath,所以我几乎可以肯定我把那部分搞砸了。顺便说一下,这是我试图从中解析数据的站点。这段代码应该只为初学者获取一个表格元素,一旦我知道该怎么做,我将解析整个表格。