{"record":{"id":"df0e08b8273cc6c6","repo":"apache/hadoop","slug":"expecting-expected-but-got-xmlstreamexception","errorCode":null,"errorMessage":"Expecting {expected}, but got XMLStreamException","messagePattern":"Expecting (.+?), but got XMLStreamException","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java","lineNumber":194,"sourceCode":"  /**\n   * Read the next tag start or end event.\n   *\n   * @param expected     The name of the next tag we expect.\n   *                     We will validate that the tag has this name,\n   *                     unless this string is enclosed in braces.\n   * @param allowEnd     If true, we will also end events.\n   *                     If false, end events cause an exception.\n   *\n   * @return             The next tag start or end event.\n   */\n  private XMLEvent expectTag(String expected, boolean allowEnd)\n      throws IOException {\n    XMLEvent ev = null;\n    while (true) {\n      try {\n        ev = events.nextEvent();\n      } catch (XMLStreamException e) {\n        throw new IOException(\"Expecting \" + expected +\n            \", but got XMLStreamException\", e);\n      }\n      switch (ev.getEventType()) {\n      case XMLEvent.ATTRIBUTE:\n        throw new IOException(\"Got unexpected attribute: \" + ev);\n      case XMLEvent.CHARACTERS:\n        if (!ev.asCharacters().isWhiteSpace()) {\n          throw new IOException(\"Got unxpected characters while \" +\n              \"looking for \" + expected + \": \" +\n              ev.asCharacters().getData());\n        }\n        break;\n      case XMLEvent.END_ELEMENT:\n        if (!allowEnd) {\n          throw new IOException(\"Got unexpected end event \" +\n              \"while looking for \" + expected);\n        }\n        return ev;","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java#L176-L212","documentation":"OfflineImageReconstructor is the ReverseXML processor (`hdfs oiv -p ReverseXML -i img.xml -o fsimage`, dispatched at OfflineImageViewerPB.java:230-232) that converts an OIV XML dump back into a binary protobuf fsimage. expectTag pulls the next StAX XMLEvent; any XMLStreamException there - unclosed tag, bad entity, encoding break - is wrapped in this IOException together with the tag the parser was waiting for. It almost always means the input is not well-formed XML.","triggerScenarios":"ReverseXML on an XML file that was truncated (a `hdfs oiv -p XML` pipe/redirect that broke), hand-edited and left unbalanced, saved with the wrong charset, or mangled by sed/awk transformations between generation and reconstruction.","commonSituations":"Streaming XML through unbuffered pipes in CI; operators renaming paths/quotas by hand and breaking well-formedness; XML from a different oiv version whose escaping the parser rejects.","solutions":["Check well-formedness first: xmllint --noout fsimage.xml and regenerate with `hdfs oiv -p XML -i <fsimage> -o img.xml` if it fails","Inspect the wrapped XMLStreamException cause - it carries the exact line and column of the break","If edits are unavoidable, use an XML-aware editor or DOM library instead of line-oriented tools","Re-run the XML and ReverseXML steps from the same Hadoop version pair"],"exampleFix":"<!-- before: unclosed root -->\n<fsimage><NameSection><namespaceId>1</namespaceId></NameSection>\n\n<!-- after -->\n<fsimage><NameSection><namespaceId>1</namespaceId></NameSection></fsimage>","handlingStrategy":"try-catch","validationCode":"# Reject non-well-formed XML before ReverseXML\ntmp=$(mktemp); xmllint --noout fsimage.xml 2> \"$tmp\" || {\n  cat \"$tmp\"; echo \"regenerate: hdfs oiv -p XML -i <fsimage> -o fsimage.xml\"; exit 1; }\nhdfs oiv -p ReverseXML -i fsimage.xml -o fsimage_rebuilt","typeGuard":null,"tryCatchPattern":"try {\n  OfflineImageReconstructor.run(\"fsimage.xml\", \"fsimage_rebuilt\");\n} catch (IOException e) {\n  System.err.println(\"XML rejected while expecting: \" + e.getMessage());\n  Throwable c = e.getCause();\n  if (c instanceof XMLStreamException) {\n    System.err.println(\"at line \" + ((XMLStreamException) c).getLocation().getLineNumber()\n        + \" column \" + ((XMLStreamException) c).getLocation().getColumnNumber());\n  }\n}","preventionTips":["Treat oiv XML as machine-generated: regenerate rather than repair","Pipe every generated/edited XML through xmllint --noout in scripted pipelines","Keep the XML-producing oiv and the ReverseXML oiv on the same Hadoop version"],"tags":["hdfs","reversexml","malformed-xml","fsimage","offline-image-viewer"],"backgroundTag":"malformed-xml","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}