0

I am new to jsoup so I am a little bit confused as to how to apply the modification to the original HTML file and then get it as an output.

After giving changes by selecting the portion of html

e.g. Element elements = doc.select("_____").attr("_____",____); (As this elements would only have the selected portion...)

how can I apply this to the original doc? so that I can get the modified HTML as an output?

Thank you very much

1 Answer 1

6

The changes are applied to the doc as you make them. For example, I start off with this:

String html = "<html>" +
        "<body>" +
        "<p class=\"class1\">p1</p>" +
        "<p class=\"class2\">p2</p>" +
        "</body>" +
        "</html>";

Document doc = Jsoup.parse(html);
System.out.println(doc);

It outputs:

<html>
 <head></head>
 <body>
  <p class="class1">class1</p>
  <p class="class2">class2</p>
 </body>
</html>

Now I make some changes to the p elements:

Element p1 = doc.select("p.class1").first();
p1.attr("class", "classOne");

Element p2 = doc.select("p.class2").first();
p2.attr("id", "helloworld");

System.out.println(doc);

The output is different to reflect the changes I made to its Elements:

<html>
 <head></head>
 <body>
  <p class="classOne">p1</p>
  <p class="class2" id="helloworld">p2</p>
 </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.