2014-01-07 335 views
0

我有一个巨大的字符串,它是通过JSOUP.I取得的字符串完整的html。使用String Bufer替换API(替换(int startIndex,int endIndex, “要改变字符串)。该字符串缓冲区填充perfectly.But当我尝试更换新的字符串缓冲区的HTML的子这是行不通的。用一个StringBuffer子字符串替换一个子字符串

这里是代码片段。

html = html.replace(divStyle1.trim(), heightwidthM.toString().trim()); 

最初的大html是

<!DOCTYPE html> 
<html xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" class="SAF" id="global-header-light"> 
<head> 

</head> 
<body> 


**<div style="background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto;">**      

<div style="height:2058px; padding-left:0px; padding-top:36px;"> 


<iframe style="height:90px; width:728px;"/> 



</div> 
</div> 

</body> 
</html> 

的divStyle1字符串是

background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto; 

和字符串缓冲器具有值

背景图像:网址(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat:不重复; -webkit-背景尺寸:1001PX 2059px;高度:720像素;宽度:900px; text-align:center;保证金:0汽车;

不起作用,其中divStyle是最后一个HTML的字符串(在String中),heightwidthM是必须替换的Stringbuffer值。它不会抛出任何错误,但它也不会改变它。

感谢 斯瓦拉杰

+0

这是什么值?如果'html'很大,只需发布​​字符串的相关部分。你确定有完全匹配吗? – rgettman

+2

考虑到您使用的是jsoup,并且jsoup让您的生活更轻松地处理html文档,为什么不从您的html创建'org.jsoup.nodes.Document',找到您需要的标签并对其进行修改? –

+0

@rgettman进行了必要的编辑 –

回答

1

这是很容易与JSoup

String html = "<!DOCTYPE html>\n<html xmlns:og=\"http://opengraphprotocol.org/schema/\" xmlns:fb=\"http://www.facebook.com/2008/fbml\" xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" class=\"SAF\" id=\"global-header-light\">\n<head>\n\n</head>\n<body>\n\n\n**<div style=\"background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height: 2059px; width: 1001px; text-align: center; margin: 0 auto;\">**      \n\n<div style=\"height:2058px; padding-left:0px; padding-top:36px;\">\n\n\n<iframe style=\"height:90px; width:728px;\"/>\n\n\n\n</div>\n</div>\n\n</body>\n</html>"; 
String newStyle = "background-image: url(http://aka-cdn-ns.adtech.de/rm/ads/23274/HPWomenLOFT_1381687318.jpg);background-repeat: no-repeat;-webkit-background-size: 1001px 2059px; height:720px; width:900px; text-align: center; margin: 0 auto;"; 

Document document = Jsoup.parse(html); 
document.body().child(0).attr("style", newStyle); 
System.out.println(document.html()); 
+0

非常感谢... –

0

说回我的建议,如果你不介意尝试,你可以做这样的事情:

Document newDocument = Jsoup.parse(<your html string>, StringUtils.EMPTY, Parser.htmlParser()); 
Elements yourStyles = newDocument.select("div[style]"); // this will select all div with attributes style 
yourStyles.get(0).attr("style", <your new value>); // this will get your first div and replace attribute style to your new value 
System.out.println(newDocument.outerHtml()); 
相关问题