Search code examples
javaxmlstax

STAX XMLStreamWriter doesn't write with small files


I have a little program using STAX which copy the content of a XML file to another. By the way, I discovered an enigmatic problem with XMLStreamWriter.

When I try to write many elements, the writing works. But when I try to write few elements, it doesn't works (the file is empty).

For example, this code works (3000 elements):

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileOutputStream("toto.xml"));

    writer.writeStartDocument();
    for(int i = 0; i < 3000; ++i) {
        writer.writeStartElement("toto");
        writer.writeEndElement();
    }
    writer.writeEndDocument();

And this code doesn't work (50 elements):

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new FileOutputStream("toto.xml"));

    writer.writeStartDocument();
    for(int i = 0; i < 50; ++i) {
        writer.writeStartElement("toto");
        writer.writeEndElement();
    }
    writer.writeEndDocument();

Have you any idea ?


Solution

  • writer.close();
    

    ;) Typical behaviour.