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");
}
}