2014-01-07 327 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()); 
相關問題