0

hello every one i have a very big challenge tody , is to split a big xml file that contains for example 2000 elements (eg) :

   `<bigxml>
    <element><small>AAA<small/><small>BBB<small/><small>CCC<small/></element>
    <element><small>DDD<small/><small>EEE<small/><small>FFF<small/></element>
    <element><small>GGG<small/><small>HHH<small/><small>III<small/></element>
    .......
    .......
   </bigxml>`

the result is multiple files with 100 element per file without losing the header

'<bigxml>    </bigxml>' per file !

how to proceede please ?

1 Answers1

0

Well, the simplest thing to do is to read the file line by line and have a counter to make sure that when you read 100 lines, write it into a file with opening and closing bigxml tag and continue iteration. A simple nested loop should do the trick.

Do something like

while(scanner.hasNextLine()){
    // Here open a new file 
    for(int i=0;i<100;i++){
         String s = scanner.nextLine();
        // write string into the file
    }
    // Close the file
}
anirudh
  • 4,116
  • 2
  • 20
  • 35
  • ok thaks for your reply ,and i'll do it by DOM or SAX or if you know another framework to simplify the work ? thanks again . – user3484177 Apr 01 '14 at 08:40
  • DOM and SAX parsers are used to parse the xml file to get individual elements or attributes from them, and using them here would only make the process more complex...Since you just want to split the file based on lines, this would be the simplest way – anirudh Apr 01 '14 at 08:43
  • all right , because i was writing dom an sax and yes it was very complex . – user3484177 Apr 01 '14 at 08:46
  • @anirddh : can you please give a demo ? – user3484177 Apr 01 '14 at 09:11
  • another issue is that if the file is not well formed like : ' AAABBBCCC DDDEEEFFF GGGHHHIII' but in another format the algorithm will not work correctly ? what to do ? – user3484177 Apr 01 '14 at 09:39
  • You mean if each element is not in a single line?? – anirudh Apr 01 '14 at 09:43
  • In that case, you just read the entire file into a string first, use `replaceAll()` to replace each `` with `\n` and then use that string – anirudh Apr 01 '14 at 09:52
  • Please have a look at http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file to read file contents into a string and then look at http://docs.oracle.com/javase/7/docs/api/java/lang/String.html?is-external=true to know how to use the `replaceAll()` method. try and understand the java docs, as this will help you become a better programmer. – anirudh Apr 01 '14 at 09:58