1

Before I was using this code to write an XML file using StAX:

 // Create a XMLOutputFactory
 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
 // Create XMLEventWriter
 XMLEventWriter eventWriter = outputFactory
        .createXMLEventWriter(new FileOutputStream(behaviorsFilePath));
 // Create a EventFactory
 XMLEventFactory eventFactory = XMLEventFactory.newInstance();
 XMLEvent end = eventFactory.createDTD("\n");
 // Create and write Start Tag
 StartDocument startDocument = eventFactory.createStartDocument();
 eventWriter.add(startDocument);
 eventWriter.add(end);
 eventWriter.add(end);

 // Create config open tag
 StartElement configStartElement = eventFactory.createStartElement("",
          "", ROOT);
//...

This code was working fine but now it throws me an javax.xml.stream.XMLStreamException: Trying to write multiple DOCTYPE declarations exception.

When debugging the application, I am able to see that the exception is thrown when I call the second time: eventWriter.add(end);

Why this code was working before and now it isn't?

UPDATE:

I'm not sure if this is really important but this code is running in a SwingWorker thread...

The only modification that I made that could be a problem is that I added libraries to the project to use Axis2 Web Services (one of them is: geronimo-stax-api_1.0_spec-1.0.1.jar)... And I saw this:

Speed - Axis2 uses its own object model and StAX (Streaming API for XML) parsing to achieve significantly greater speed than earlier versions of Apache Axis.

And this:

stax-api-1.0.1.jar (The StAX API's that contain the javax.xml.namespace.QName class. This jar may be replaced by any other jar that contains the javax.xml.namespace.QName implementation. However Axis2 uses this class from the stax-api-1.0.1.jar which comes bundled with the Axis2 distribution)

4

1 回答 1

1

Because, like the exception tells you already, you are "Trying to write multiple DOCTYPE declarations". Why does your code have eventWriter.add(end); twice?

于 2012-10-18T14:38:41.897 回答