1

I have a html string:

<p>1</p> <p><img src="/filename.jpg" /></p> 
<p>2</p> <p><img src="/filename.jpg" alt="1324" width="600" height="180" /></p>
<p>3</p> <p><img style="border-width: 1px;" src=/filename.jpg" alt="" width="1000" height="300" /></p>
<p>4</p> <p><img style="border-width: 1px; max-width:100%" src=/filename.jpg" alt="" width="1000" height="300" /></p>

I need to search for all img tags in this string. If it doesn't have that property, insert style="max-width:100%" as an attribute to them. If that img tag already has the style attribute, then I need to add property "max-width:100%". I use Java.

How to accomplish this?

3
  • where is your code? Commented Mar 8, 2017 at 9:23
  • I only have an example string <p>1</p> <p><img src="/filename.jpg" /></p> <p>2</p> <p><img src="/filename.jpg" alt="1324" width="600" height="180" /></p> <p>3</p> <p><img style="border-width: 1px;" src=/filename.jpg" alt="" width="1000" height="300" /></p> <p>4</p> <p><img style="border-width: 1px; max-width:100%" src=/filename.jpg" alt="" width="1000" height="300" /></p> Commented Mar 8, 2017 at 9:28
  • Have a look at jsoup.org, it's a HTML parser lib for java. Commented Mar 8, 2017 at 9:35

1 Answer 1

4

Use the Jsoup lib.
Download Jar or using mvn.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * Created by hwding on 3/8/17.
 */
public class Bar {
    public static void main(String[] args) {
        String string = "<p>1</p> <p><img src=\"/filename.jpg\" /></p> <p>2</p> <p><img src=\"/filename.jpg\" alt=\"1324\" width=\"600\" height=\"180\" /></p> <p>3</p> <p><img style=\"border-width: 1px;\" src=/filename.jpg\" alt=\"\" width=\"1000\" height=\"300\" /></p> <p>4</p> <p><img style=\"border-width: 1px; max-width:100%\" src=/filename.jpg\" alt=\"\" width=\"1000\" height=\"300\" /></p>";

        Document document = Jsoup.parse(string);
        Elements elements = document.select("img");

        elements.forEach(e -> {
            if(!e.hasAttr("style"))
                e.attr("style", "max-width:100%");
            System.out.println(e.toString());
        });
    }
}

Output:

<img src="/filename.jpg" style="max-width:100%">
<img src="/filename.jpg" alt="1324" width="600" height="180" style="max-width:100%">
<img style="border-width: 1px;" src="/filename.jpg&quot;" alt="" width="1000" height="300">
<img style="border-width: 1px; max-width:100%" src="/filename.jpg&quot;" alt="" width="1000" height="300">
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I aslo need to check if that img tag already has the style attribute, then I need to add property "max-width:100%" for style. But I already done it with Jsoup.

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.