{"record":{"id":"68e09fcd0074465e","repo":"pentaho/pentaho-kettle","slug":"error-writing-line-xmloutput","errorCode":null,"errorMessage":"Error writing line :","messagePattern":"Error writing line :","errorType":"exception","errorClass":"KettleStepException","httpStatus":null,"severity":"error","filePath":"plugins/xml/core/src/main/java/org/pentaho/di/trans/steps/xmloutput/XMLOutput.java","lineNumber":280,"sourceCode":"  private Object getNullIfValue( Object valueData, String nullString ) {\n    if ( valueData == null && !Utils.isEmpty( nullString ) && isNullIfFieldValueAllowed() ) {\n      return nullString;\n    }\n    return valueData;\n  }\n\n  private void writeField( ValueMetaInterface valueMeta, Object valueData, String element ) throws KettleStepException {\n    try {\n      String value = valueMeta.getString( valueData );\n      if ( value != null ) {\n        data.writer.writeStartElement( element );\n        data.writer.writeCharacters( value );\n        data.writer.writeEndElement();\n      } else {\n        data.writer.writeEmptyElement( element );\n      }\n    } catch ( Exception e ) {\n      throw new KettleStepException( \"Error writing line :\", e );\n    }\n  }\n\n  public String buildFilename( boolean ziparchive ) {\n    return meta.buildFilename( this, getCopy(), data.splitnr, ziparchive );\n  }\n\n  public boolean openNewFile() {\n    boolean retval = false;\n    data.writer = null;\n\n    try {\n      if ( meta.isServletOutput() ) {\n        data.writer = XML_OUT_FACTORY.createXMLStreamWriter( getTrans().getServletPrintWriter() );\n        if ( meta.getEncoding() != null && meta.getEncoding().length() > 0 ) {\n          data.writer.writeStartDocument( meta.getEncoding(), \"1.0\" );\n        } else {\n          data.writer.writeStartDocument( Const.XML_ENCODING, \"1.0\" );","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/plugins/xml/core/src/main/java/org/pentaho/di/trans/steps/xmloutput/XMLOutput.java#L262-L298","documentation":"XMLOutput.writeField wraps exceptions from writing a single field's XML element (writeCharacters/writeEmptyElement) into a KettleStepException with the generic message 'Error writing line :'. The root cause is in the wrapped exception, usually a StAX writer failure or illegal character data.","triggerScenarios":"writeRowToFile -> writeField when data.writer.writeCharacters or writeEmptyElement throws, e.g. stream already closed, invalid XML chars in the value, or null element name from a misconfigured field.","commonSituations":"Illegal control characters in database strings being dumped to XML; output file handle invalidated by OS-level issues; element name empty because output field config is broken.","solutions":["Inspect the cause chain for the real StAX/XMLStreamException message","Strip invalid XML characters from field values before the XML Output step","Verify output fields are fully configured with valid element names","Check the output file/stream is writable and not prematurely closed"],"exampleFix":"// before\nvalue = valueMeta.getString( valueData );\n// after\nvalue = XmlHandler.addEncodedValue( valueMeta.getString( valueData ) ); // sanitize for XML","handlingStrategy":"try-catch","validationCode":"for (XMLField f : meta.getOutputFields()) { if (f.getFieldName() == null || f.getFieldName().isEmpty()) throw new IllegalStateException(\"Empty element name\"); }","typeGuard":"boolean hasValidElement = fieldName != null && !fieldName.trim().isEmpty();","tryCatchPattern":"try { writeField(rowMeta, row, i); } catch (KettleStepException e) { log.error(\"Field write failed: \" + e.getCause(), e); }","preventionTips":["Strip invalid XML control characters upstream (Replace in string / UDF)","Keep element names non-empty and XML-safe","Test with sample data containing special characters","Enable detailed step logging to capture the StAX cause"],"tags":["kettle","xml","io"],"backgroundTag":"file-write-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}