Tuesday, January 6, 2015

extract xml data from log file

have a case that xml content has been put in log file,while I search log file,I need extract those data out of the log file,xml data is correct formatted and cross multiple lines in log file.
normal linux tool is not working with multiple line data extract and xml data.

have to create a java code to extract data i want.



import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class Test {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(args[0]);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        boolean print=false;
        while ((line = br.readLine()) != null) {
            if (line.equals("<WatchList>")){
                print=true;
            }
            if(print){
                System.out.println(line);
            }
            if(line.equals("</WatchList>")){
                print=false;
            }
            br.close();
        }

}

}


No comments:

Post a Comment