用于在Android中解析HTML的XmlPullParser或JSoup
我有一个包含html代码的字符串,我只想从中提取某些元素 我已经在google和stack overflow周围搜索了一段时间,似乎大多数建议的解决方案都是使用Android XmlPullParser或JSoup用于在Android中解析HTML的XmlPullParser或JSoup,android,parsing,Android,Parsing,我有一个包含html代码的字符串,我只想从中提取某些元素 我已经在google和stack overflow周围搜索了一段时间,似乎大多数建议的解决方案都是使用Android XmlPullParser或JSoup 以下哪种方法最适合在android中解析hmtl?为什么?JSOUP将是显而易见的选择,因为您可以轻松识别HTML元素,并从中创建对象。最后,您可以执行所需的操作,将文档转换为字符串,并最终在视图中进行设置 下面是一个代码片段,我需要删除我的网页的页眉和页脚,然后在我的视图中显示它
以下哪种方法最适合在android中解析hmtl?为什么?JSOUP将是显而易见的选择,因为您可以轻松识别HTML元素,并从中创建对象。最后,您可以执行所需的操作,将文档转换为字符串,并最终在视图中进行设置 下面是一个代码片段,我需要删除我的网页的页眉和页脚,然后在我的视图中显示它
if(document!=null) {
if (document.getElementById("header").getElementById("site-head") != null) {
document.getElementById("header").getElementById("site-head").remove();
}
if (document.getElementById("footer") != null) {
document.getElementById("footer").remove();
} if (document.getElementsByClass("fs-footer-newsletter") != null) {
document.getElementsByClass("fs-footer-newsletter").remove();
}
String modifiedDocument = document.toString();
modifiedDocument = modifiedDocument.replace("<html lang=\"en-US\" prefix=\"og: http://ogp.me/ns#\" class=\"no-js\">","<html lang=\"en-US\" prefix=\"og: http://ogp.me/ns#\" class=\"no-js\" style=\"margin-top:0 !important;\">");
modifiedDocument = modifiedDocument.replace("<header id=\"header\">","<header id=\"header\" style=\"margin-top:-16px;\">");
modifiedDocument = modifiedDocument.replace("<ul class=\"main-filters\">","<ul class=\"main-filters\" style=\"top:0;\">");
Document d = Jsoup.parse(modifiedDocument);
WebSettings ws = getItemWebview.getSettings();
ws.setJavaScriptEnabled(true);
Log.i("modifiedDocument",document.toString());
if(isURLForShop) {
loadingImg.setVisibility(View.GONE);
getItemWebview.loadDataWithBaseURL(storeUrl, d.toString(), "text/html", "utf-8", "");
}else{
loadingImg.setVisibility(View.GONE);
getItemWebview.loadDataWithBaseURL(restyleDenimURL, d.toString(), "text/html", "utf-8", "");
}
}
if(文档!=null){
if(document.getElementById(“页眉”).getElementById(“站点页眉”)!=null){
document.getElementById(“标头”).getElementById(“站点标头”).remove();
}
if(document.getElementById(“footer”)!=null){
document.getElementById(“footer”).remove();
}if(document.getElementsByClass(“fs页脚新闻稿”)!=null){
document.getElementsByClass(“fs页脚新闻稿”).remove();
}
字符串modifiedDocument=document.toString();
modifiedDocument=modifiedDocument.replace(“,”);
modifiedDocument=modifiedDocument.replace(“,”);
modifiedDocument=modifiedDocument.replace(“”,“”;
文档d=Jsoup.parse(modifiedDocument);
WebSettings ws=getItemWebview.getSettings();
ws.setJavaScriptEnabled(true);
Log.i(“modifiedDocument”,document.toString());
如果(iUrlForShop){
加载img.setVisibility(View.GONE);
getItemWebview.loadDataWithBaseURL(storeUrl,d.toString(),“text/html”,“utf-8”,““”);
}否则{
加载img.setVisibility(View.GONE);
getItemWebview.loadDataWithBaseURL(restyleDenimURL,d.toString(),“text/html”,“utf-8”,“utf-8”);
}
}
发布您的代码或字符串@我的弦是什么有什么关系?这是一个带有网页html源代码的字符串。谢谢。我还发现,我可以使用JSoup直接从URL获取html代码,因此它大大简化了我获取html字符串的方式:DDone:p在timelimitHi@Tirafesi出现之前,我无法这样做,怎么做?你能描述一下你是怎么做的吗?