Java JSoup 删除元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16446358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:57:07  来源:igfitidea点击:

JSoup Remove Elements

javajsoup

提问by akshayb

Even though, this may sound too basic, I would like to ask how do I remove an element from doc using Jsoup.

尽管这听起来太基本了,但我想问一下如何使用 Jsoup 从文档中删除元素。

I tried searching for it, but no success.

我试图寻找它,但没有成功。

Here is problem:

这是问题:

Elements myNewElements = doc.getElementsByAttribute("hello");

//Now I need to perform some other methods on myNewElements before removing.
//Hence..suggested method says,
doc.getElementsByAttribute("hello").remove();

This works fine. But I believe selecting same elements again and again could prove memory hungry. Is it possible ?

这工作正常。但我相信一次又一次地选择相同的元素可能会证明内存不足。是否可以 ?

doc.select(myNewElements).remove();

//Try to select myNewElements from doc.

//尝试从文档中选择 myNewElements。

采纳答案by Francisco Paulo

If you didn't add any new elements that match your inital select, you don't need to select the elements again.

如果您没有添加任何与初始选择相匹配的新元素,则无需再次选择这些元素。

Each element in elements has a reference to its parent and the remove() method just tells the parent to remove that child element.

element 中的每个元素都有对其父元素的引用,而 remove() 方法只是告诉父元素删除该子元素。

In essence, just doing:

本质上,只是做:

myNewElements.remove()

should work.

应该管用。

回答by Francisco Paulo

Better loop over the elements and remove them within:

更好地循环元素并在其中删除它们:

for( Element element : doc.select(myNewElements) )
{
    element.remove();
}

There's a similar question: Parse html with jsoup and remove the tag block

还有一个类似的问题:Parse html with jsoup and remove the tag block