2013-05-29 27 views
1

我對Java編程比較陌生,並且正在嘗試創建一個可以幫助一些同事的應用程序。Java,如何從大文件中提取一些文本並將其導入到較小的文件中

我想要做的事情的背景是,讀取大型文件的內容,最多可能超過400,000行,其中包含XML但不是有效的XML文檔,因爲它是一種日誌。

我所要做的,是建立在用戶輸入一個唯一的ID的應用程序,這然後掃描文件找到,如果它存在,如果這樣做,而且往往是唯一的ID在生產中出現幾次XML,那麼我想向後遍歷節點ID <documentRequestMessage>,然後將所有節點從該節點複製到其關閉節點,並將其放入自己的文檔中。

我知道如何創建新文檔,但我正努力找出如何從本質上'查找倒退'並將所有內容複製到結束標記,非常感謝任何幫助。

編輯

不幸的是,我一直無法弄清楚如何迄今實施的任意的三點建議。

correlationId是前面提到的獨特參考。

當前的代碼我有,其工作方式和結果輸出到控制檯,是

String correlationId = correlationID.getText(); 
BufferedReader bf = new BufferedReader(new FileReader(f)); 
System.out.println("Looking for " + correlationId); 
int lineCount = 0; 
String line; 

while ((line = bf.readLine()) != null) { 
    lineCount++; 
    int indexFound = line.indexOf(correlationId); 

    if (indexFound > -1) { 
     System.out.println("Found CorrelationID on line " + "\t" + lineCount + "\t" + line); 
    } 
} 

bf.close(); 

任何進一步的幫助greatfully讚賞,我不要求別人能把它寫對我來說,只是一些真明確基本指令:)請

EDIT 2

我試圖讀取並可以發現提取該文件的副本here

+0

問題 - 你怎麼知道它不是有效的XML?你能發表一個關於它的「無效」的樣本嗎? –

+0

@SeanBright我知道它不是有效的XML的原因,是因爲 1)XMLSpy不會驗證它。 2)它包含多個<?xml version =「1.0」encoding =「UTF-8」?>(585個條目)! 3)另外還有我不認爲是正確的XML註釋的評論,例如[2013-05-29 12:18:57,626]默認:4'#DocumentCompositionLogger sca.component.mediation.java.Custom1322734159344 INFO - requestDocumentProductionPackG02請求有效載荷收到>>>>>>我已經複製一個文件到這裏,所以你可以看到我正在嘗試閱讀的整個文檔,可以在這裏找到(http://db.tt/Sw0C4tWL) – Chris

+0

@ Gilbert的建議看起來很直截了當,你能告訴你實施它有什麼麻煩嗎? – Vitaliy

回答

1

在您通過文件向前閱讀尋找您的唯一ID時,請保留對您遇到的最近的documentRequestMessage的引用。當您找到唯一的ID時,您將擁有需要提取該消息的參考。

在這種情況下,「參考」可能意味着一些事情。由於您沒有遍歷DOM(因爲它不是有效的XML),所以您可能只需將該位置存儲在documentRequestMessage所在的文件中。如果您使用的是FileInputStream(或支持mark的任何InputStream),則只需mark/reset即可存儲並返回到消息啓動文件的位置。

這是我相信你正在尋找的實現。這讓很多基於您鏈接的日誌文件的假設,但它的工作原理示例文件:

private static void processMessages(File file, String correlationId) 
{ 
    BufferedReader reader = null; 

    try { 
     boolean capture = false; 
     StringBuilder buffer = new StringBuilder(); 
     String lastDRM = null; 
     String line; 

     reader = new BufferedReader(new FileReader(file)); 

     while ((line = reader.readLine()) != null) { 
      String trimmed = line.trim(); 

      // Blank lines are boring 
      if (trimmed.length() == 0) { 
       continue; 
      } 

      // We only actively look for lines that start with an open 
      // bracket (after trimming) 
      if (trimmed.startsWith("[")) { 
       // Do some house keeping - if we have data in our buffer, we 
       // should check it to see if we are interested in it 
       if (buffer.length() > 0) { 
        String message = buffer.toString(); 

        // Something to note here... at this point you could 
        // create a legitimate DOM Document from 'message' if 
        // you wanted to 

        if (message.contains("documentRequestMessage")) { 
         // If the message contains 'documentRequestMessage' 
         // then we save it for later reference 
         lastDRM = message; 
        } else if (message.contains(correlationId)) { 
         // If the message contains the correlationId we are 
         // after, then print out the last message with the 
         // documentRequestMessage that we found, or an error 
         // if we never saw one. 
         if (lastDRM == null) { 
          System.out.println(
            "No documentRequestMessage found"); 
         } else { 
          System.out.println(lastDRM); 
         } 

         // In either case, we're done here 
         break; 
        } 

        buffer.setLength(0); 
        capture = false; 
       } 

       // Based on the log file, the only interesting messages are 
       // the ones that are DEBUG 
       if (trimmed.contains("DEBUG")) { 
        // Some of the debug messages have the XML declaration 
        // on the same line, and some the line after, so let's 
        // figure out which is which... 
        if (trimmed.endsWith("?>")) { 
         buffer.append(
           trimmed.substring(
            trimmed.indexOf("<?"))); 
         buffer.append("\n"); 
         capture = true; 
        } else if (trimmed.endsWith("Message:")) { 
         capture = true; 
        } else { 
         System.err.println("Can't handle line: " + trimmed); 
        } 
       } 
      } else { 
       if (capture) { 
        buffer.append(line).append("\n"); 
       } 
      } 
     } 
    } catch (IOException ex) { 
     ex.printStackTrace(System.err); 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException ex) { 
       /* Ignore */ 
      } 
     } 
    } 
} 
+0

根據您的意見更新。 –

0

你可以做的是閱讀文件的內容,並尋找<documentRequestMessage>元素。當您找到上述元素之一時,請閱讀,直至找到</documentRequestMessage>並將其存儲在列表中,以便列表中的所有documentRequestMessage都可用。

您可以在最後或在添加到列表中時遍歷此列表以找到您要查找的唯一標識。如果您發現它寫入XML文件或忽略。

0

我假設你的日誌是一系列<documentRequestMessage>的內容。

根本不掃描日誌。

閱讀日誌,並且每次遇到<documentRequestMessage>標題時,開始將該<documentRequestMessage>塊的內容保存到塊區域中。

我不確定您是否必須解析XML,或者您可以將它保存爲字符串列表。

當你遇到一個</documentRequestMessage>預告片,查看是否塊的ID,你正在尋找,

的ID相匹配。如果ID匹配,寫<documentRequestMessage>塊到輸出文件。如果ID不匹配,請清除塊區域並讀取下一個<documentRequestMessage>標題。

這樣,你的文件閱讀中就沒有回溯。

相關問題