Question

I would like to remove an img from a html code. This html code is generated by a soft. But the src of my img depends on the name of the html document.

For example if the name of my html code is test.html The src img will be

<img srx="test_one_way.gif">

If the name is example.html

The src img will be

<img srx="example_one_way.gif">

As you see, just a part of the src is changing.

_one_way.gif won't change

So my question is : How can I select(remove) this img ?

To my mind, may be I could select it with : "name"+_one_way.gif

I"m parsing the doc like that :

Document doc=Jsoup.parse(new FileInputStream(readLine("file name: ")), "ISO-8859-1", "", Parser.xmlParser());

But I don't see how I can do it.

Was it helpful?

Solution

The easiest would be to select the img using expression in jsoup if you don't assign it a unique parameter:

Elements elements = doc.select("img[src$=_one_way.gif");
Element firstElement = elements.first();

Then, you can always change an elements attribute:

firstElement.attr("src", firstElement.attr("src").replaceFirst("^(.*?)(_one_way\\.gif)$", "$1") + "_one_way.gif");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top