gpt4 book ai didi

java - 将日志文件解析为 XML

转载 作者:行者123 更新时间:2023-12-02 12:42:26 24 4
gpt4 key购买 nike

我这里有一个代码,它接受文件的输入,并仅在控制台上显示包含单词“LANTALK”的某些行,然后将这些行写出到外部文件。我需要的是能够过滤行内的信息以某种方式显示。

完整代码如下:

import java.io.*;
import java.util.*;

public class baseline
{

// Class level variables
static Scanner sc = new Scanner(System.in);

public static void main(String[] args) throws IOException,
FileNotFoundException { // Start of main

// Variables
String filename;

// Connecting to the output file with a buffer
PrintWriter outFile = new PrintWriter(
new BufferedWriter(
new FileWriter("chatOutput.log")));

// Get the input file
System.out.print("Please enter full name of the file: ");
filename = sc.next();

// Assign the name of the input file to a file object
File log = new File(filename);
String textLine = null; // Null
String outLine = ""; // Null
BufferedWriter bw = null;



try
{
// assigns the input file to a filereader object
BufferedReader infile = new BufferedReader(new FileReader(log));

sc = new Scanner(log);
while(sc.hasNext())
{
String line=sc.nextLine();
if(line.contains("LANTALK"))
System.out.println(line);
} // End of while


try
{
// Read data from the input file
while((textLine = infile.readLine()) != null)
{
// Print to output file
outLine = textLine;
sc = new Scanner (outLine);
while(sc.hasNext())
{
String line=sc.nextLine();
if(line.contains("LANTALK"))
outFile.printf("%s\n",outLine);
}// end of while
} // end of while
} // end of try


finally // This gets executed even when an exception is thrown
{
infile.close();
outFile.close();
} // End of finally
} // End of try


catch (FileNotFoundException nf) // Goes with first try
{
System.out.println("The file \""+log+"\" was not found");
} // End of catch
catch (IOException ioex) // Goes with second try
{
System.out.println("Error reading the file");
} // End of catch

} // end of main

} // end of class

这是输入文件的示例行:

08:25:26.668 [D] [T:000FF4] [F:LANTALK2C] <CMD>LANMSG</CMD>
<MBXID>1124</MBXID><MBXTO>5760</MBXTO><SUBTEXT>LanTalk</SUBTEXT><MOBILEADDR>
</MOBILEADDR><LAP>0</LAP><SMS>0</SMS><MSGTEXT>but didn't give me the info I
needed</MSGTEXT>
08:25:26.672 [+] [T:000FF4] [S:1:1:1124:5607:5] LANMSG [0/2 | 0]

这是我试图让输出看起来像这样:

8:25:00 AM [Steve Jobs] to [John Smith] but didn't give me the info I needed

有人对执行此操作的最佳方法有任何建议吗?我正在考虑某种 XML 解析器,但正在读取的文件是 .log,并且我不确定如何在这种情况下转换它,因为它已经被读取了。谢谢!

最佳答案

您需要一种混合方法:从缓冲读取器读取包含 LANTALK 的行,然后从第一个 < 到最后一个 > 存储在字符串生成器中。之后 jsoup 可以为你完成 xml 技巧

编辑

从这里下载 jsoup

https://mvnrepository.com/artifact/org.jsoup/jsoup/1.8.3

然后在你的if block 中使用String indexOf和lastIndexOf,直到你读取了日志行中的xml(将其放入字符串生成器中)

关于java - 将日志文件解析为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44890516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com