Skip to main content

XMLStreamException: Can not write DOCTYPE declaration (DTD) when not in prolog any more (state 2; start element(s) written)

No replies
szhb69
Offline
Joined: 2009-07-15
Points: 0

This is the code snippet:
public void createDocument(OutputStream stream) throws XMLStreamException {
logger.fine("Header#createDocument() entering");

XMLOutputFactory outFactory = XMLOutputFactory.newFactory();
outFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
Boolean.valueOf(true));
XMLEventWriter eventWriter = outFactory.createXMLEventWriter(stream);
XMLEventFactory eventFactory = XMLEventFactory.newFactory();

this.namespaces = new ArrayList();
this.namespaces.add(eventFactory.createNamespace("p",
"http://www.w3.org/1999/xhtml"));
this.namespaces.add(eventFactory.createNamespace("p1",
"http://www.w3.org/1999/xhtml/datatypes"));
this.namespaces.add(eventFactory.createNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance"));

Attribute attribute = eventFactory.createAttribute("xsi:schemaLocation",
ReqIF_Const.SCHEMA_LOC + ReqIF_Const.REQ_IF_XSD_REF);
List attrList = Collections.singletonList(attribute);

StartDocument startDoc = eventFactory.createStartDocument(CHAR_SET);
eventWriter.add(startDoc);
eventWriter.add(ReqIF_Const.NL);

StartElement startReqIF = eventFactory.createStartElement(
ReqIF_Const.REQIF_PREFIX, ReqIF_Const.REQ_IF_XSD_REF,
DOC_TAG, attrList.iterator(),
this.namespaces.iterator());
try {
eventWriter.add(startReqIF);
eventWriter.add(ReqIF_Const.NL);
eventWriter.flush();
} catch (Exception e) {
logger.warning("Header#createDocument() exception is: " + e.getMessage());
}
logger.fine("Header#createDocument() exit");
}
It mus be said, it works fine, if the code is called manually from a JUnit test class.
It works also fine, if the test class is started by maven, but only if maven starts the specific module!
If maven runs a site test over all existing modules, the XMLStream Exception is thrown! There is no multithreading in use, so I can not understand why the same code runs or do not!

The outcome when started indivudually is:
<?xml version="1.0" encoding="UTF-8"?>
http://www.omg.org/spec/ReqIF/20110401/reqif.xsd">
...
This is rather simple, as programming just started!

any hint is welcome!