简体   繁体   English

将文本文件标签解析为xml-Java

[英]Parse text file tags as xml - Java

I have a logs text file consists of several tags like the following: 我有一个日志文本文件,由几个标签组成,如下所示:

<log level="info" id="12144" ....> <msg> test msg </msg> </log>

I want to parse this file, therefore I thought that since it looks like XML I can add the xml ( <?xml version="1.0"? >) and a root ( <file> ) tags to the file and then parse it with DOM Parser. 我想解析此文件,因此我认为,由于它看起来像XML,因此可以将xml( <?xml version="1.0"? >)和根( <file> )标记添加到文件中,然后使用DOM解析器。

But currently, I am facing an issue to add text at the beginning of the file, since all available options are to overwrite, or to copy the original file to a temporary location and then add the intended text, which is not applicable for me as I am dealing with files bigger than 100 MB. 但是目前,我面临一个在文件开头添加文本的问题,因为所有可用的选项都将被覆盖,或者将原始文件复制到一个临时位置,然后添加想要的文本,这不适用于我我正在处理大于100 MB的文件。

I use the below code to try to add the text at the beginning, and I tried several solutions or classes other than RandomAccessFile

RandomAccessFile RAF = new RandomAccessFile(file, "rw");
        RAF.getChannel().position(0);

        RAF.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n <file> \r\n").getBytes()); 

        RAF.close();

Do you have any suggestions to add the text at the beginning or to parse the text file as it is and extract elements? 您是否有任何建议在开头添加文本或按原样解析文本文件并提取元素?

THanks, 谢谢,

如果您知道条目的精确模式, 如果该模式不会再更改它的相对容易提取使用正则表达式,如条目:

"<log *level=\"([^\"]*)\" *id=\"([^\"])\" *> ..."

Can't you just get the content of the file as String or Stream or whatever and then add your stuff to the beginning? 您能否仅以StringStream或其他形式获取文件内容,然后将内容添加到开头? After that you still can parse the document String/Stream whatever 之后,您仍然可以解析文档String/Stream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM