Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace one element with list of elements using jsoup?

I just want to replace one element with different list of elements, I can't find a way how to do it. Where dynamicContentHtmls is list of Elements and element is the one which should be replaced.

My experimental code:

int i=0;
    for (Element element: dynamicContents){
        //element.remove();
        element.append(dynamicContentHtmls.get(i));
        //TextNode text = new TextNode(dynamicContentHtmls.get(i), "");
        //element.replaceWith(text);
        //element.html().replaceAll(element.html(),  
        //dynamicContentHtmls.get(i));
        i++;
    }
like image 535
Ondrej Tokar Avatar asked Sep 28 '22 07:09

Ondrej Tokar


People also ask

What is Jsoup element?

A HTML element consists of a tag name, attributes, and child nodes (including text nodes and other elements). From an Element, you can extract data, traverse the node graph, and manipulate the HTML.

What is Jsoup parse?

jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.

What is a node in Jsoup?

A node is the generic name for any type of object in the DOM hierarchy. An element is one specific type of node. The JSoup class model reflects this: Node.


1 Answers

I have solved it by doing this:

element.parent().append(dynamicContentHtmls.get(i));
element.remove();
like image 143
Ondrej Tokar Avatar answered Oct 06 '22 19:10

Ondrej Tokar