0

I have an xml file and there are contents inside the tags. I am trying to read this file in java. I am parsing through the contents of the file word by word. I want to divide the file into chunks. For example if I found a string code=”34076-0” I want to start writing the next token from the found string to another file till another code=”1234-5” is found . However, I am not able to stop the file parsing or collect the contents after the string has matched to write it to another file. I have tried the following code.

try {

            Scanner sc2 = null;    

            sc2 = new Scanner(new File("A:/OneDrive - PharmaCompany, Inc/Diksha Work/lipitorx.xml"));

            while(sc2.hasNextLine())

            {

                   String s = sc2.next();



                   if(sc2.next()==("code=\"34076-0\"" ))

                       {                         

                              // Here I want to write the contents of the file after matching the code=34076-0 till the next code into another file



                       }                                                 

                      System.out.println(s);     



            }





   } catch (Exception e) {

          // TODO: handle exception

          System.out.println("Unable to open the file");

   }



   }
S.Diksha
  • 69
  • 6
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kevin Anderson Jun 07 '18 at 19:16
  • break the while loop where you have put in the comment. then in another while loop keep copying the content will end condition is met. – gagan singh Jun 07 '18 at 19:17

0 Answers0