{"record":{"id":"a0d7cc9b9672e802","repo":"apache/hadoop","slug":"sax-error","errorCode":null,"errorMessage":"SAX error: {}","messagePattern":"SAX error: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineEditsViewer/XmlEditsVisitor.java","lineNumber":80,"sourceCode":"    this.out = out;\n    try {\n      factory = org.apache.hadoop.util.XMLUtils.newSecureSAXTransformerFactory();\n      TransformerHandler handler = factory.newTransformerHandler();\n      handler.getTransformer().setOutputProperty(OutputKeys.METHOD, \"xml\");\n      handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, \"UTF-8\");\n      handler.getTransformer().setOutputProperty(OutputKeys.INDENT, \"yes\");\n      handler.getTransformer().setOutputProperty(XML_INDENTATION_PROP,\n              XML_INDENTATION_NUM);\n      handler.getTransformer().setOutputProperty(OutputKeys.STANDALONE, \"yes\");\n      handler.setResult(new StreamResult(out));\n      contentHandler = handler;\n      \n      contentHandler.startDocument();\n      contentHandler.startElement(\"\", \"\", \"EDITS\", new AttributesImpl());\n    } catch (TransformerConfigurationException e) {\n      throw new IOException(\"SAXTransformer error: \" + e.getMessage());\n    } catch (SAXException e) {\n      throw new IOException(\"SAX error: \" + e.getMessage());\n    }\n  }\n\n  /**\n   * Start visitor (initialization)\n   */\n  @Override\n  public void start(int version) throws IOException {\n    try {\n      contentHandler.startElement(\"\", \"\", \"EDITS_VERSION\", new AttributesImpl());\n      StringBuilder bld = new StringBuilder();\n      bld.append(version);\n      addString(bld.toString());\n      contentHandler.endElement(\"\", \"\", \"EDITS_VERSION\");\n    }\n    catch (SAXException e) {\n      throw new IOException(\"SAX error: \" + e.getMessage());\n    }","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineEditsViewer/XmlEditsVisitor.java#L62-L98","documentation":"XmlEditsVisitor's constructor emits startDocument() and the opening <EDITS> element through the transformer handler; this IOException wraps a SAXException from those first callbacks. It means the serialization pipeline failed at the very start of output — usually an unusable output stream (the StreamResult target) rather than bad edit-log data.","triggerScenarios":"new XmlEditsVisitor(out) where the OutputStream passed as StreamResult is already closed, null-wrapped, or fails on first write; a transformer handler left in a bad state from a prior failure.","commonSituations":"Passing a stream obtained from an already-closed FileOutputStream; reusing a visitor or output target after an earlier error; wrapper streams closed prematurely by application code.","solutions":["Verify the OutputStream is open and writable before constructing the visitor (write and reset a test byte).","Open the output file and construct the visitor right before use, not long-lived across failures.","Build a fresh XmlEditsVisitor per output file; never reuse instances across runs.","If it persists on a valid stream, investigate the JAXP environment (see the SAXTransformer error)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"private static OutputStream openWritable(File f) throws IOException {\n  OutputStream out = new FileOutputStream(f);\n  out.write(' ');   // probe write\n  out.flush();\n  new RandomAccessFile(f, \"rw\").setLength(0);  // reset probe\n  return out;\n}","typeGuard":null,"tryCatchPattern":"try {\n  visitor = new XmlEditsVisitor(out);\n} catch (IOException e) {\n  // constructor failed writing <EDITS>; check stream/disk before retrying with a fresh one\n  out.close();\n  throw e;\n}","preventionTips":["Open the output stream immediately before constructing the visitor.","Never pass an already-closed stream as the visitor target.","Construct a fresh visitor per output file; do not pool or reuse."],"tags":["hdfs","offline-edits-viewer","xml","sax","output-stream"],"backgroundTag":"saxexception","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}