apache/maven · error · XMLStreamException

Expected root element '${rootTag}' but found no element at a

Error message

Expected root element '${rootTag}' but found no element at all: invalid XML document

What it means

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.

Source

Thrown at src/mdo/reader-stax.vm:295

                } else if (parsed) {
                    // fallback, already expected a XMLStreamException due to invalid XML
                    throw new XMLStreamException("Duplicated tag: '${rootTag}'", parser.getLocation(), null);
                }
#if ( $locationTracking )
                $rootLcapName = parse${rootUcapName}(parser, strict, parser.getNamespaceURI(), inputSrc);
#elseif ( $needXmlContext )
                $rootLcapName = parse${rootUcapName}(parser, strict, parser.getNamespaceURI(), context);
#else
                $rootLcapName = parse${rootUcapName}(parser, strict, parser.getNamespaceURI());
#end
                parsed = true;
            }
            eventType = parser.next();
        }
        if (parsed) {
            return $rootLcapName;
        }
            throw new XMLStreamException("Expected root element '${rootTag}' but found no element at all: invalid XML document", parser.getLocation(), null);
    } //-- ${root.name} read(XMLStreamReader, boolean)

#foreach ( $class in $model.allClasses )
 #if ( $class.name != "InputSource" && $class.name != "InputLocation" )
  #set ( $classUcapName = $Helper.capitalise( $class.name ) )
  #set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
  #set ( $ancestors = $Helper.ancestors( $class ) )
  #set ( $allFields = $Helper.xmlFields( $class ) )
  #if ( $locationTracking )
    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, String namespace, InputSource inputSrc) throws XMLStreamException {
  #elseif ( $needXmlContext )
    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, String namespace, Deque<Object> context) throws XMLStreamException {
  #else
    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict, String namespace) throws XMLStreamException {
  #end
        String tagName = parser.getLocalName();
        ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
  #if ( $locationTracking )

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Check the input is non-empty (file length or first bytes) before parsing.
  2. Peek for a root element; if none exists, reject with a clearer upstream error naming the file instead of parsing.
  3. Fix the producer that wrote the empty file.
  4. If the document is legitimately optional, branch on emptiness and skip the read.

Example fix

// before
Model model = reader.read(new FileInputStream(file), true); // file is 0 bytes

// after
if (file.length() == 0) {
    throw new IllegalArgumentException(file.getPath() + " is empty");
}
Model model = reader.read(new FileInputStream(file), true);
Defensive patterns

Strategy: validation

Validate before calling

if (file.length() == 0) {
    throw new IllegalArgumentException(file.getPath() + " is empty");
}
// for streams: buffer once, check, then parse from the buffer
byte[] bytes = in.readAllBytes();
if (bytes.length == 0) {
    throw new IllegalArgumentException("empty XML input");
}
Model model = reader.read(new ByteArrayInputStream(bytes), true);

Try / catch

try {
    Model model = reader.read(in, true);
} catch (XMLStreamException e) {
    if (e.getMessage() != null && e.getMessage().contains("found no element at all")) {
        // empty document: report which file/stream it was
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21). Data as JSON: /api/errors/482f9d986acf10d2. Report an issue: GitHub.