2014-01-16 21 views
0

請幫助我使用java 一個棘手的算法將大型html文件拆分爲多個html文件。我試過高達limit.please幫我如何根據java中的字符長度將HTML文件分割爲多個

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
<link href="template.css" rel="stylesheet" type="text/css"/> 
<link href="page-template.xpgt" rel="stylesheet" type="application/vnd.adobe-page-template+xml"/> 
</head> 
<body> 
<div class="story"> 
<p class="cn">2</p> 
<p class="img"><img src="images/common.jpg" alt=""/></p> 
<p class="ct"> some text!</p> 
<p class="tx"><span class="dropcap"> some text</span> some text!</p> 
<p class="tx"> some text!</p> 
<p class="img"><img src="images/ch02-fig1.jpg" alt=""/></p> 
<p class="tx"> some text some text some text some text.</p> 
<p class="img"><img src="images/ch02-fig2.jpg" alt=""/></p> 
<p class="tx"> some text some text some text some text </p> 
<p class="tx"> some text some text some text </p> 
<p class="tx"> some text some text some text some text.</p> 
<p class="img"><img src="images/ch02-fig3.jpg" alt=""/></p> 
<p class="tx"> some text!</p> 
<p class="tx"> 
</p> 
</div> 
</body> 
</html> 

這是根據的

計數我的HTML文件中的一些文本

HTML文件應該被分裂!

+0

對於我們應該如何拆分HTML,您有任何「標準」嗎? 一個例子肯定會有所幫助。 – Incognito

+0

問題更新 – jenuine

回答

1
You can use the following logic .... 
List<String> lines = Files.readAllLines(FileSystems.getDefault() 
        .getPath("yourhtmlfile"), 
        StandardCharsets.UTF_8);  
      for (String htmlData : lines) 
      { 
       Pattern splitPattern = Pattern 
         .compile(sometext_pattern); 
       Matcher match = splitPattern.matcher(htmlData); 

       while (match.find()) 
       {  
        String lineToBeSplit = match.group();  

       } 

          . 
          . 

      } 

"lineToBeSplit" will have the split data. 
1

你的問題很模糊:)。

關於分割字符串(在這種情況下爲html): 最簡單的方法是將html文件作爲文本讀入到String中,然後使用String.split()方法將字符串分割爲所需的正則表達式。例如.split(「/ div」)會給你一個粗略的方法,你的html將被分解成「div」(假設你甚至在你的html中有div)。但是,這對於嵌套div會非常不利。

關於讀寫文件:Reading a plain text file in Java 此外,你會發現網絡上的html解析器hackload很可能在你的情況下工作十倍。

+0

我的所有問題都將非自然:)將試試感謝 – jenuine

相關問題