Reger05
Reger05

Reputation: 223

XMLEventWriter not writing to output stream

I am new to stackoverflow so hopefully I format this question correctly! I have searched through the site (and google) for an answer to my problem but haven't been able to find a reason why this isn't working for me.

I am trying to read in an event from an XMLEventReader and write it out to an XMLEventWriter (simple enough right?) It is not outputting any of the events to either a file, or even simply to System.out. Any hints would be great =)

public void parse(InputStream is) throws XMLStreamException, Exception {
    XMLEventReader reader = null;
    try {

        XMLInputFactory inputFact = XMLInputFactory.newInstance();
        XMLStreamReader streamReader = inputFact.createXMLStreamReader(is);
        reader = inputFact.createXMLEventReader(streamReader);
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new
        FileOutputStream("C:\\temp\\results\\exceltestresults.xml"));
        //or System.out
        while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        eventWriter.add(event);
        }
        ...........

When I debug through the code I can see each of the events being read in the While{} loop.

Upvotes: 2

Views: 1231

Answers (1)

M A
M A

Reputation: 72874

You missed eventWriter.close();

    while (reader.hasNext()) {
      XMLEvent event = reader.nextEvent();
      eventWriter.add(event);
    }
    eventWriter.close();

It is better also to close these resources (reader, streamReader and eventWriter) in a finally block.

Upvotes: 1

Related Questions