![](/img/trans.png)
[英]How to select an 'outer' HTML element with a certain tag within it in JSOUP?
[英]Jsoup - how to select elements within an element
我試圖例如在另一個元素內選擇一個元素:
<video>
<source src = "" />
</video>
用於在視頻視頻案例中選擇源元素的Android代碼不具有src屬性
private String getVideoSrc(Document doc){
String src = null;
Element video = doc.select("video").first();
Element source = null;
if(video.attr("src") == null){
source = video.select("source").first();
}
if(video.attr("src") !=null){
src = video.attr("src");
}else if(source.attr("src") != null){
src = source.attr("src");
}
return src;
}
當視頻中有src屬性時,它會正常返回內容(如果不存在),但是在視頻中的源中存在它甚至不會獲得標簽
例:
Element video = doc.select("video").first();
Element source = video.select("source").first();
這是帶有不起作用的元素的頁面,但是我正在構建的應用程序必須獲得最大可能站點數的src屬性。
在if條件中使用isEmpty()
,而不要使用null
。
String src = null;
Element video = doc.select("video").first();
Element source = null;
if(video.attr("src").isEmpty()){
System.out.println("is null");
source = video.select("source").first();
}
if(!video.attr("src").isEmpty()){
src = video.attr("src");
}else if(!source.attr("src").isEmpty()){
src = source.attr("src");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.