{"record":{"id":"42a5013bf0a9ca4b","repo":"flowable/flowable-engine","slug":"error-writing-bpmn-xml","errorCode":null,"errorMessage":"Error writing BPMN XML","messagePattern":"Error writing BPMN XML","errorType":"exception","errorClass":"XMLException","httpStatus":null,"severity":"error","filePath":"modules/flowable-bpmn-converter/src/main/java/org/flowable/bpmn/converter/BpmnXMLConverter.java","lineNumber":609,"sourceCode":"            }\n\n            BPMNDIExport.writeBPMNDI(model, xtw);\n\n            // end definitions root element\n            xtw.writeEndElement();\n            xtw.writeEndDocument();\n\n            xtw.flush();\n\n            outputStream.close();\n\n            xtw.close();\n\n            return outputStream.toByteArray();\n\n        } catch (Exception e) {\n            LOGGER.error(\"Error writing BPMN XML\", e);\n            throw new XMLException(\"Error writing BPMN XML\", e);\n        }\n    }\n\n    protected void createXML(FlowElement flowElement, BpmnModel model, XMLStreamWriter xtw) throws Exception {\n\n        if (flowElement instanceof SubProcess subProcess) {\n\n            if (flowElement instanceof Transaction) {\n                xtw.writeStartElement(ELEMENT_TRANSACTION);\n            } else if (flowElement instanceof AdhocSubProcess) {\n                xtw.writeStartElement(ELEMENT_ADHOC_SUBPROCESS);\n            } else {\n                xtw.writeStartElement(ELEMENT_SUBPROCESS);\n            }\n\n            xtw.writeAttribute(ATTRIBUTE_ID, subProcess.getId());\n            if (StringUtils.isNotEmpty(subProcess.getName())) {\n                if (!(options.isSaveElementNameWithNewLineInExtensionElement() && BpmnXMLUtil.containsNewLine(subProcess.getName()))) {","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-bpmn-converter/src/main/java/org/flowable/bpmn/converter/BpmnXMLConverter.java#L591-L627","documentation":"convertToXML(BpmnModel, OutputStream) catches any Exception raised while writing the model to StAX XMLStreamWriter and wraps it as XMLException(\"Error writing BPMN XML\"). This is the serialization direction: the in-memory BpmnModel could not be written out as BPMN 2.0 XML.","triggerScenarios":"Calling convertToXML(model, out) where a converter's createXML throws (I/O error on the underlying OutputStream, XMLStreamException, or an exception thrown while serializing a particular flow element).","commonSituations":"Writing to a closed/full OutputStream, disk-full when serializing to a file stream, or a model containing elements that fail during XML generation (e.g. illegal characters in names/IDs from programmatic model edits).","solutions":["Inspect the logged stack trace ('Error writing BPMN XML') for the real cause.","Ensure the target OutputStream is open, writable, and not closed before conversion finishes.","Validate model element names/IDs/strings for illegal XML characters before calling convertToXML.","Check disk space or stream capacity if writing to a file or in-memory buffer."],"exampleFix":"// before\nOutputStream out = Files.newOutputStream(path); out.close(); // closed early\nconverter.convertToXML(model, out);\n// after\ntry (OutputStream out = Files.newOutputStream(path)) {\n    converter.convertToXML(model, out);\n}","handlingStrategy":"try-catch","validationCode":"// Sanity-check the model and target stream before serializing\nif (model == null || model.getMainProcess() == null) {\n    throw new IllegalStateException(\"Nothing to serialize: empty BpmnModel\");\n}\nif (!outCanAcceptMore) throw new IOException(\"Target stream unavailable\");","typeGuard":null,"tryCatchPattern":"try {\n    byte[] xml = converter.convertToXML(model, out);\n} catch (XMLException e) {\n    log.error(\"BPMN serialization failed; cause:\", e.getCause());\n    throw e;\n}","preventionTips":["Keep the OutputStream open until convertToXML returns (use try-with-resources at the call site)","Check disk space when writing to files","Sanitize programmatically-set element names/IDs for illegal XML characters"],"tags":["java","xml","serialization","bpmn"],"backgroundTag":"json-marshal-failed","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}