的Java读取HTML文件,并保存其内容到一个Excel文件

问题描述:

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结构,用'分裂(“

+0

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

+0

@TuyenNguyen,我不能使用split(“”),因为有时候这个值还包含一个空格,如果我用空格拆分,它也会拆分我想要的值。 (例如,免费复制和8/3/2017 5:44:51 AM) –

对于您的解决方案很简单,只需使用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代码。你应该知道你想获得关于阅读内容的帮助,那么你必须准确地发布内容 –