开发者

changing the data of the element

I need to change some information in an HTML file and I managed to reach to the elements using JSOUP. However, I faced a problem when trying to modify the following style e开发者_开发技巧lement:

<style type="text/css">
#leftimage {
    background: #FFFCEF 
        url("/image1.jpg");
}

</style>

I used the following code

Element txt=doc.select("style").first();
String t=txt.data();
String s=" #leftimage { background: #FFFCEF url('/image1.jpg');}";
txt.data().replace(t, s);

but nothing changed! Why isn't the color changing when I do this?


String in Java is immutable. You cannot change it. In your case replace() does not change existing text but RETURNS new text with replaced data (read the Javadoc for it).

Actually looking at what you want to do, running replace also does not make too much sense (it substitues any occurence of t with s inside the string you run it on). You basically want to replace the whole text of your element, so you most likely need to do something like:

txt.text(" #leftimage { background: #FFFCEF url('/image1.jpg');}");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜