{"record":{"id":"482f9d986acf10d2","repo":"apache/maven","slug":"expected-root-element-roottag-but-found-no-el","errorCode":null,"errorMessage":"Expected root element '${rootTag}' but found no element at all: invalid XML document","messagePattern":"Expected root element '(.+?)' but found no element at all: invalid XML document","errorType":"exception","errorClass":"XMLStreamException","httpStatus":null,"severity":"error","filePath":"src/mdo/reader-stax.vm","lineNumber":295,"sourceCode":"                } else if (parsed) {\n                    // fallback, already expected a XMLStreamException due to invalid XML\n                    throw new XMLStreamException(\"Duplicated tag: '${rootTag}'\", parser.getLocation(), null);\n                }\n#if ( $locationTracking )\n                $rootLcapName = parse${rootUcapName}(parser, strict, parser.getNamespaceURI(), inputSrc);\n#elseif ( $needXmlContext )\n                $rootLcapName = parse${rootUcapName}(parser, strict, parser.getNamespaceURI(), context);\n#else\n                $rootLcapName = parse${rootUcapName}(parser, strict, parser.getNamespaceURI());\n#end\n                parsed = true;\n            }\n            eventType = parser.next();\n        }\n        if (parsed) {\n            return $rootLcapName;\n        }\n            throw new XMLStreamException(\"Expected root element '${rootTag}' but found no element at all: invalid XML document\", parser.getLocation(), null);\n    } //-- ${root.name} read(XMLStreamReader, boolean)\n\n#foreach ( $class in $model.allClasses )\n #if ( $class.name != \"InputSource\" && $class.name != \"InputLocation\" )\n  #set ( $classUcapName = $Helper.capitalise( $class.name ) )\n  #set ( $classLcapName = $Helper.uncapitalise( $class.name ) )\n  #set ( $ancestors = $Helper.ancestors( $class ) )\n  #set ( $allFields = $Helper.xmlFields( $class ) )\n  #if ( $locationTracking )\n    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, String namespace, InputSource inputSrc) throws XMLStreamException {\n  #elseif ( $needXmlContext )\n    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, String namespace, Deque<Object> context) throws XMLStreamException {\n  #else\n    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, String namespace) throws XMLStreamException {\n  #end\n        String tagName = parser.getLocalName();\n        ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);\n  #if ( $locationTracking )","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/src/mdo/reader-stax.vm#L277-L313","documentation":"The read loop reached END_DOCUMENT without ever seeing a START_ELEMENT: the input contains no element at all. Empty files, whitespace-only input, or documents made solely of an XML declaration, comments, or processing instructions produce this. The parser location in the exception sits at the end of the document, confirming there was nothing to parse rather than a wrong root name.","triggerScenarios":"read() on a zero-byte InputStream/Reader; a file containing only whitespace; an XML declaration with nothing after it; a document of only comments; or a stream already fully consumed by earlier code.","commonSituations":"Empty pom.xml left behind by a failed code-generation or template step; placeholder files committed by accident; resource resolution returning empty content; reusing a stream after another parser drained it.","solutions":["Check the input is non-empty (file length or first bytes) before parsing.","Peek for a root element; if none exists, reject with a clearer upstream error naming the file instead of parsing.","Fix the producer that wrote the empty file.","If the document is legitimately optional, branch on emptiness and skip the read."],"exampleFix":"// before\nModel model = reader.read(new FileInputStream(file), true); // file is 0 bytes\n\n// after\nif (file.length() == 0) {\n    throw new IllegalArgumentException(file.getPath() + \" is empty\");\n}\nModel model = reader.read(new FileInputStream(file), true);","handlingStrategy":"validation","validationCode":"if (file.length() == 0) {\n    throw new IllegalArgumentException(file.getPath() + \" is empty\");\n}\n// for streams: buffer once, check, then parse from the buffer\nbyte[] bytes = in.readAllBytes();\nif (bytes.length == 0) {\n    throw new IllegalArgumentException(\"empty XML input\");\n}\nModel model = reader.read(new ByteArrayInputStream(bytes), true);","typeGuard":null,"tryCatchPattern":"try {\n    Model model = reader.read(in, true);\n} catch (XMLStreamException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"found no element at all\")) {\n        // empty document: report which file/stream it was\n    }\n    throw e;\n}","preventionTips":["Guard file-based parsing with a length/emptiness check.","Buffer streams once (readAllBytes) and parse from the buffer so emptiness is detected cheaply.","Make generators that might write empty XML files fail loudly."],"tags":["xml","stax","empty-input","parsing"],"backgroundTag":"empty-xml-document","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}