通过webview加载进来html,如何将HTML模板加载到WebView中?
您可以使用HTMLCleaner从WebView获取HTML作为DOM并通过TagNodes进行更改。一个示例如何使用HTMLCLeaner来获取节点的某个属性。你应该在AsyncTask中执行此操作,这样它就不会阻止ui。
public static String snapFromCleanedHTMLWithXPath(String stringURL, String xPath, String attrToStrip) {
String snap = "";
// create an instance of HtmlCleaner
HtmlCleaner cleaner = new HtmlCleaner();
// take default cleaner properties
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(true);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
// open a connection to the desired URL
URL url;
try {
url = new URL(stringURL);
URLConnection conn = url.openConnection();
// use the cleaner to "clean" the HTML and return it as a TagNode object
TagNode root = cleaner.clean(new InputStreamReader(conn.getInputStream()));
Object[] foundNodes = root.evaluateXPath(xPath);
if (foundNodes.length > 0) {
// casted to a TagNode
TagNode foundNode = (TagNode) foundNodes[0];
snap = foundNode.getAttributeByName(attrToStrip);
}
} 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();
}
cleaner = null;
props = null;
url = null;
return snap;
}