I’ve been looking at the new streaming API for XML (JSR-173), I’ve been generally impressed but have found a bug in the reference implementation, here’s the details, using this test program:
importjava.io.*;importjavax.xml.stream.*;publicclassStaxWriterTest{staticStringnsURI="http://ianp.org/nsURI";staticStringnsPrefix="a";staticintdepth=0;// Used to pretty print the output.staticXMLStreamWriterw;publicstaticvoidmain(String[]args){try{w.writeStartDocument();indent(1);w.writeStartElement(nsURI,"root");w.writeNamespace(nsPrefix,nsURI);indent(0);w.writeEmptyElement(nsURI,"levelOne");w.writeAttribute(nsURI,"foo","foo");indent(0);w.writeStartElement(nsURI,"levelOne");w.writeEndElement();indent(1);w.writeStartElement(nsURI,"levelOne");indent(1);w.writeEmptyElement(nsURI,"levelTwo");w.writeAttribute(nsURI,"foo","foo");indent(-2);w.writeEndElement();indent(0);w.writeStartElement(nsURI,"levelOne");w.writeEndElement();indent(-1);w.writeEndElement();w.flush();w.close();}catch(Exceptione){e.printStackTrace();System.exit(1);}}staticvoidindent(intd){try{if(d<0){depth+=d;}for(inti=0;i<depth;++i)w.writeCharacters(" ");if(d>0){depth+=d;}}catch(XMLStreamExceptione){thrownewRuntimeException(e);}}}
This is using version 7 of the reference implementation, by the way. The program should produce this output: