2017-08-04 178 views
0

HTML文件的代碼示例:的Java讀取HTML文件,並保存其內容到一個Excel文件

<HTML> 
<HEAD> 
<TITLE>REPORT</TITLE></HEAD> 
<BODY> 
<TITLE>REPORT</TITLE><PRE><H2>################ REPORT ###################</H2><H3>Setup</H3> Item1     1120          <br> Item2     Copy free         <br> Item3     8/3/2017 5:44:51 AM      <br> Item4     <Press OK>       <br> 

我需要閱讀的信息與<br>線。我們的目標是將這些信息保存到一個Excel文件像下面

enter image description here

我目前使用的BufferedReader閱讀HTML文件,但我不知道如何來分隔行包含字段和值。我試圖使用散列表來保存它的字段名稱和值,但我不能以正確的方式獲取值。我也試過Jsoup擺脫HTML標籤的,但它給了我更多的複雜性讀取線以來,HTML文件

private final String[] modStrings = new String[]{"Item1", "Item2", "Item3", "Item4", "Item5"}; 

public void readHtmlFile() throws IOException { 
     FileReader reader = new FileReader("C:\\Users\\file.html"); 
     // StringBuilder sb = new StringBuilder(); 
     BufferedReader br = new BufferedReader(reader); 
     String line; 
     String[] tempContent = {}; 
     ArrayList content = new ArrayList(); 
     HashMap modMap = new HashMap<>(); 
     while ((line=br.readLine()) != null) { 
      tempContent = line.split("<br>"); 
      for(int i = 0; i < tempContent.length; i++){ 
       for (String sub:modStrings){ 
        if(tempContent[i].contains(sub)){ 
         String value = "TODO HERE"; // TODO 
         content.add(sub); 
         modMap.put(sub, value); 
        } 
       } 

      } 
     } 
//  String textOnly = Jsoup.parse(sb.toString()).text(); 
     for(int i = 0; i < content.size(); i++){ 
      System.out.println(content.get(i)); 
      System.out.println(modMap); 
     } 
    } 

任何建議或想法將是一個很大的幫助。

+0

通過上面的HTML結構,用'分裂(「< br「)不是給你想要的。你應該使用''split'與'space'來獲得'Item'並且值 –

+0

你可以使用String [] keyVal = s.trim()。split(「+」); value = keyVal [1]; key = keyVal [0); – CodeIsLife

+0

@TuyenNguyen,我不能使用split(「」),因爲有時候這個值還包含一個空格,如果我用空格拆分,它也會拆分我想要的值。 (例如,免費複製和8/3/2017 5:44:51 AM) –

回答

0

對於您的解決方案很簡單,只需使用String類的util函數,根據您的html內容使用合適的方法獲取您想要的內容。比如我在這裏使用split(String regex),[split(String regex, int limit)](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)),修剪or subString` ......做一個簡單的一招

示例代碼爲您提供:

public static void main(String[] args) throws IOException { 
     String[] modStrings = new String[] { "Item1", "Item2", "Item3", "Item4", "Item5" }; 
     FileReader reader = new FileReader("html.html"); 
     BufferedReader br = new BufferedReader(reader); 
     String line; 
     String[] tempContent = {}; 
     ArrayList content = new ArrayList(); 
     HashMap<String, String> modMap = new HashMap<>(); 
     while ((line = br.readLine()) != null) { 
     if (line.contains("<br>")) { 
      line = line.substring(line.indexOf("Item1")); 
      tempContent = line.split("<br>"); 
      for (String item : tempContent) { 
       if (item.contains("Item")) { 
        String[] itemArr = item.trim().split(" ", 2); 
        String itemName = itemArr[0].trim(); 
        String value = itemArr[1].trim(); 
        modMap.put(itemName, value); 
       } 
      } 
     } 
     } 
     for(String key : modMap.keySet()){ 
      System.out.println(key + ":" + modMap.get(key)); 
     } 
    } 
+0

很抱歉,此代碼無法正常工作。我將我的html文件更新爲原始格式。舊的html樣本旨在提供更好的視覺效果,但我認爲這會讓人們對這個問題產生誤解,以及您在這裏的情況。如果條件不能正常工作。以及if條件中的語句。 –

+0

包含代碼但沒有解釋的答案在Stack Overflow中通常不受歡迎。你能解釋爲什麼你認爲這段代碼符合OP的要求嗎? –

+0

@MinwuYu我已經更新了新的html格式的代碼。以前的代碼不會運行,因爲您已經更改了html代碼。你應該知道你想獲得關於閱讀內容的幫助,那麼你必須準確地發佈內容 –

相關問題