This discussion is archived
4 Replies Latest reply: Jun 13, 2013 1:00 AM by user12679330 RSS

splitting multiple files from a single file into individual files(xml's)

user12679330 Newbie
Currently Being Moderated
Hi,
I have a log file(.txt) which has all the input and output xmls along with some other server log details, I want to use that as a input and then to take the required input files, say 50 xmls of input message which has unique root element and 50 xmls of output messages which has unique root element from it, ignoring the unnecessary data.

like:
input and output xml's will have the standard root elements like <input></input> and <output></output>.

Thanks.

Edited by: user12679330 on 15-May-2013 22:52

Edited by: user12679330 on 15-May-2013 22:53

Edited by: user12679330 on 15-May-2013 22:53
  • 1. Re: splitting multiple files from a single file into individual files(xml's)
    aksarben Journeyer
    Currently Being Moderated
    And your question is...?
  • 2. Re: splitting multiple files from a single file into individual files(xml's)
    user12679330 Newbie
    Currently Being Moderated
    program for that, or how best we can implement this requirement.
  • 3. Re: splitting multiple files from a single file into individual files(xml's)
    Kayaman Guru
    Currently Being Moderated
    Well, how about SAX or StaX?
  • 4. Re: splitting multiple files from a single file into individual files(xml's)
    user12679330 Newbie
    Currently Being Moderated

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;

    import java.util.StringTokenizer;

    public class ExtractXmls {

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

                String data = readFile("C:/MyFolder/DataFile.log");
                String startData = "<MyXML";

                StringTokenizer stk = new StringTokenizer(data, startData);
                int count = 0;
                while (stk.hasMoreTokens()) {
                      String token = stk.nextToken();
                      count++;
                }
                //System.out.println(count);
                String endData = "</MyXML>";
                int start = data.indexOf(startData);
                int end = data.indexOf(endData);
                end = end+ 5;
                int i = 1;
                while (i < 5000) {
                      String fileData = data.substring(start, end);
                      //System.out.println(BancsData);
                      data = data.substring(end);
                      start = data.indexOf(startData);
                      end = data.indexOf(endData);
                      end = end+ 5;
                      createFile(fileData,i);
                      i++;
                     
                }

          }

          private static void createFile(String data, int count) {
                FileOutputStream fos;
                try {
                      fos = new FileOutputStream("C:/Target/OutPut/file" + count + ".xml");
                      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));  
                bw.write(data);  
                bw.newLine();  
                bw.flush();  
                count++;
                } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                }  
                             

          }
         
          private static String readFile(String path) {
                StringBuilder sb = new StringBuilder();
                try {
                      File file = new File(path);
                      FileInputStream fis = new FileInputStream(file);
                      BufferedInputStream bis = new BufferedInputStream(fis);
                      int in = 0;
                      char inChar;
                      do {
                            in = bis.read();
                            inChar = (char) in;
                            if (in != -1) {
                                  sb.append(inChar);
                            }
                      } while ((in != -1));
                      bis.close();
                } catch (FileNotFoundException e) {
                      e.printStackTrace();
                } catch (IOException ioe) {
                      ioe.printStackTrace();
                }
                return sb.toString();

          }

    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points