0

I am trying to split an XML file using the following method, it created the first split file no problem but when i use output.clear(); to clear the Array of XMLEvents that already have been written it throws an error.

error: Exception in thread "main" javax.xml.stream.XMLStreamException: No element was found to write: java.lang.ArrayIndexOutOfBoundsException: -1

i tried to investigate this issue with no success, any advice on what is causing the error or how to overcome it greatly appreciated!

public void SplitBySize() throws FileNotFoundException, XMLStreamException, IOException {

    //File Path
    String filePath = "C:\\Users\\thamm\\Desktop\\XMLFile\\Data2.xml";

    //Read XML file.
    Reader fileReader = new FileReader(filePath);

    //Get XMLInputFactory instance.
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

    //Create XMLEventReader object.
    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(fileReader);

    long charCount = 0;
    int fileNumber = 1;

    while (xmlEventReader.hasNext()) {

        XMLEvent event = xmlEventReader.nextEvent();
        charCount++;
        output.add(event);

        if (charCount == MAX_SIZE) {

            XMLEventWriter xmlEventWriter = factory.createXMLEventWriter(new FileWriter("C:\\Users\\thamm\\Desktop\\SplitFiles\\output_part_" + fileNumber + ".xml"));

            for (XMLEvent i : output) {
                xmlEventWriter.add(i);
            }

            xmlEventWriter.close();
            output.clear();
            charCount = 0;
            fileNumber++;

        }

        if (event.isEndDocument()) {

            XMLEventWriter xmlEventWriter = factory.createXMLEventWriter(new FileWriter("C:\\Users\\thamm\\Desktop\\SplitFiles\\output_part_End.xml"));

            for (XMLEvent i : output) {
                xmlEventWriter.add(i);
            }
            xmlEventWriter.close();
            output.clear();
        }
    }
}

1 Answers1

0

Assuming that output is a List defined in your class, I think your problem is that you use output.clear() twice.

The first time, you use output.clear() is OK because you add an element before.

output.add(event);

But the second time, output is empty.

Solution

  • You need to check if output is empty before using output.clear() with something like output.isEmpty()

  • You maybe forgot to add elements into your list before clearing it.

Mickael
  • 4,458
  • 2
  • 28
  • 40
  • Hi Mickaël, thanks for you reply .. i have debugged the program to see if the output is empty on the second loop but its is successfully cleared and XMLEvents are being added again. – Tommy Hamm Apr 20 '17 at 10:01
  • What is the line that causing the exception ? – Mickael Apr 20 '17 at 10:08
  • for (XMLEvent i : output) { xmlEventWriter.add(i); } The problem seems to be here .. i have added xmlWriter.flush(); to see each output as i debug and the program begins to output The Elements to the second file but then the error is thrown after 2 elements are outputted and the program terminates – Tommy Hamm Apr 20 '17 at 10:17