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

Same empty-document guard as reader-stax.vm: the read loop in read(XMLStreamReader, strict) reached END_DOCUMENT with parsed still false - no START_ELEMENT was ever seen. Empty files, whitespace-only input, or declaration/comment-only documents produce this; the parser location marks the end of the document.

Source

Thrown at src/mdo/reader.vm:455

        int eventType = parser.getEventType();
        boolean parsed = false;
        while (eventType != XMLStreamReader.END_DOCUMENT) {
            if (eventType == XMLStreamReader.START_ELEMENT) {
                if (strict && ! "${rootTag}".equals(parser.getLocalName())) {
                    throw new XMLStreamException("Expected root element '${rootTag}' but found '" + parser.getLocalName() + "'", parser.getLocation(), null);
                } else if (parsed) {
                    // fallback, already expected a XMLStreamException due to invalid XML
                    throw new XMLStreamException("Duplicated tag: '${rootTag}'", parser.getLocation(), null);
                }
                $rootLcapName = parse${rootUcapName}(parser, strict);
                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 = [] )
  #foreach ( $cl in $ancestors )
    #set ( $dummy = $allFields.addAll( $cl.getFields($version) ) )
  #end
    private ${classUcapName} parse${classUcapName}(XMLStreamReader parser, boolean strict)
            throws IOException, XMLStreamException {
        String tagName = parser.getLocalName();
        ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
        for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
            String name = parser.getAttributeLocalName(i);
            String ns = parser.getAttributeNamespace(i);

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Check input non-emptiness before parsing.
  2. Peek for a root element and reject empties with a file-specific error.
  3. Fix the producer writing empty files; skip optional documents.

Example fix

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

// 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

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: identify and report the source file
    }
    throw e;
}

Prevention

When it happens

Trigger: read() on a zero-byte stream; a file with only whitespace or only an <?xml declaration; a stream already consumed by an earlier parse; comments-only content.

Common situations: Empty placeholder files; failed generation steps writing empty XML; drained streams reused for a second read.

Related errors


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