{"record":{"id":"ffbb0ebd058ae207","repo":"apache/maven","slug":"parser-must-be-on-start-element-to-read-next-text","errorCode":null,"errorMessage":"parser must be on START_ELEMENT to read next text","messagePattern":"parser must be on START_ELEMENT to read next text","errorType":"exception","errorClass":"XMLStreamException","httpStatus":null,"severity":"error","filePath":"src/mdo/reader-stax.vm","lineNumber":676,"sourceCode":"            int next = parser.next();\n            switch (next) {\n                case XMLStreamReader.SPACE:\n                case XMLStreamReader.COMMENT:\n                case XMLStreamReader.PROCESSING_INSTRUCTION:\n                case XMLStreamReader.CDATA:\n                case XMLStreamReader.CHARACTERS:\n                    continue;\n                case XMLStreamReader.START_ELEMENT:\n                case XMLStreamReader.END_ELEMENT:\n                    return next;\n            }\n        }\n    } //-- int nextTag(XMLStreamReader)\n\n    private String nextText(XMLStreamReader parser, boolean strict) throws XMLStreamException {\n        int eventType = parser.getEventType();\n        if (eventType != XMLStreamReader.START_ELEMENT) {\n            throw new XMLStreamException(\"parser must be on START_ELEMENT to read next text\", parser.getLocation(), null);\n        }\n        eventType = parser.next();\n        StringBuilder result = new StringBuilder();\n        while (true) {\n            if (eventType == XMLStreamReader.CHARACTERS || eventType == XMLStreamReader.CDATA) {\n                result.append(parser.getText());\n            } else if (eventType == XMLStreamReader.ENTITY_REFERENCE) {\n                String val = null;\n                if (strict) {\n                    throw new XMLStreamException(\"Entities are not supported in strict mode\", parser.getLocation(), null);\n                } else if (addDefaultEntities) {\n                    val = DefaultEntitiesHolder.DEFAULT_ENTITIES.get(parser.getLocalName());\n                }\n                if (val != null) {\n                    result.append(val);\n                } else {\n                    result.append(\"&\").append(parser.getLocalName()).append(\";\");\n                }","sourceCodeStart":658,"sourceCodeEnd":694,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/src/mdo/reader-stax.vm#L658-L694","documentation":"nextText(parser, strict), the generated helper for reading an element's text content, requires the parser to be positioned on that element's START_ELEMENT. Generated call sites only invoke it right after recognizing a start tag, so this invariant failure almost always means the parser handed to read() was in an unexpected state, or the STAX implementation misreports events.","triggerScenarios":"Passing an XMLStreamReader already advanced beyond the initial document state (e.g. you peeked the root element yourself, then reused the same parser for read()); reusing one parser instance across reads; custom XMLStreamReader wrappers that drop or reorder events.","commonSituations":"Pre-inspecting documents with the same parser instance handed to the reader; filtering stream readers that swallow events; non-standard STAX providers picked up via ServiceLoader.","solutions":["Create a fresh XMLStreamReader from buffered input for every read() call and leave it untouched before the call.","Do any pre-inspection with a separate parser over the same byte buffer.","Use the JDK default or Woodstox factory; check the classpath for conflicting javax.xml.stream implementations."],"exampleFix":"// before: peek the root with the same parser, then reuse it\nXMLStreamReader parser = factory.createXMLStreamReader(in);\nwhile (parser.next() != XMLStreamReader.START_ELEMENT) { /* peek */ }\nnew MavenXpp3Reader().read(parser, true); // parser state is no longer fresh\n\n// after: peek on a separate parser over buffered bytes\nbyte[] bytes = in.readAllBytes();\nString root = peekRootLocalName(new ByteArrayInputStream(bytes));\nModel model = new MavenXpp3Reader().read(new ByteArrayInputStream(bytes), true); // fresh parser","handlingStrategy":"try-catch","validationCode":"// Prevention: buffer once, always hand read() a brand-new parser\nbyte[] bytes = in.readAllBytes();\nXMLStreamReader parser = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));\nModel model = reader.read(parser, true);","typeGuard":null,"tryCatchPattern":"try {\n    Model model = reader.read(parser, true);\n} catch (XMLStreamException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"parser must be on START_ELEMENT\")) {\n        // parser misuse or broken STAX implementation, not bad input\n        throw new IllegalStateException(\"parser handed to read() was not at its initial state\", e);\n    }\n    throw e;\n}","preventionTips":["Never hand read() a partially consumed parser; buffer input and create a fresh parser.","Keep pre-inspection and model reading on separate parser instances.","Standardize on one STAX implementation across the classpath."],"tags":["xml","stax","parser-state","internal-invariant"],"backgroundTag":"stax-parser-state-invalid","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}