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?
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"" alt="" width="1000" height="300">
<img style="border-width: 1px; max-width:100%" src="/filename.jpg"" alt="" width="1000" height="300">