{"record":{"id":"e819629f3c1fbc7b","repo":"apache/hadoop","slug":"got-unxpected-characters-while-looking-for-expect","errorCode":null,"errorMessage":"Got unxpected characters while looking for {expected}: {ev.asCharacters().getData()}","messagePattern":"Got unxpected characters while looking for (.+?): (.+?)","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":202,"sourceCode":"   *\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;\n      case XMLEvent.START_ELEMENT:\n        if (!expected.startsWith(\"[\")) {\n          if (!ev.asStartElement().getName().getLocalPart().\n                equals(expected)) {\n            throw new IOException(\"Failed to find <\" + expected + \">; \" +\n                \"got \" + ev.asStartElement().getName().getLocalPart() +\n                \" instead.\");\n          }","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java#L184-L220","documentation":"expectTag tolerates whitespace-only CHARACTERS events between tags but throws when it finds non-whitespace character data where a tag was expected. In oiv XML, text only ever appears inside leaf value elements, so stray character data means a value lost its wrapping tag or text was pasted at the wrong nesting level - typically the residue of hand edits or a broken transform.","triggerScenarios":"ReverseXML where non-whitespace text sits directly inside a container element (e.g. loose text inside <INodeSection> next to <inode> children), a value moved outside its leaf tag, or an append/merge that left a fragment between elements. The message includes the offending character data, which pinpoints the break.","commonSituations":"Manual edits to rename paths or adjust quotas that paste values at the wrong level; diff/patch applications that drop a tag but keep its text; scripted text substitutions that eat a delimiter tag.","solutions":["Use the offending data named in the message (or xmllint) to locate the text, then re-wrap it in the correct leaf element or delete it","Regenerate the XML from the original fsimage and redo the edit with a DOM/XPath tool","Never edit oiv XML with sed/awk - use a library that preserves structure"],"exampleFix":"<!-- before: stray value inside a container -->\n<INodeSection><numInodes>1</numInodes>16400<inode>...</inode></INodeSection>\n\n<!-- after -->\n<INodeSection><numInodes>1</numInodes><inode><id>16400</id>...</inode></INodeSection>","handlingStrategy":"validation","validationCode":"# Container sections must not carry non-whitespace text directly\nxmllint --xpath 'string(//INodeSection/text()[normalize-space()])' fsimage.xml 2>/dev/null |\n  grep -q '^$' || { echo 'stray text inside a section - re-wrap or remove it'; exit 1; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Edit with DOM/XPath libraries that keep values inside their leaf tags","After bulk edits, diff text nodes of containers against a clean dump","Prefer regenerating the XML over patching misplaced values"],"tags":["hdfs","reversexml","xml-text-content","fsimage"],"backgroundTag":"xml-unexpected-characters","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}