{"record":{"id":"264578ae0df83c32","repo":"Tencent/matrix","slug":"unexpected-start-tag-found-parser-getname-e","errorCode":null,"errorMessage":"Unexpected start tag: found ${parser.getName()}, expected ${firstElementName}","messagePattern":"Unexpected start tag: found (.+?), expected (.+?)","errorType":"exception","errorClass":"XmlPullParserException","httpStatus":null,"severity":"error","filePath":"matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/utils/PowerProfile.java","lineNumber":665,"sourceCode":"\n    public double getBatteryCapacity() {\n        return getAveragePower(POWER_BATTERY_CAPACITY);\n    }\n\n    @SuppressWarnings(\"StatementWithEmptyBody\")\n    static class XmlUtils {\n        public static void beginDocument(XmlPullParser parser, String firstElementName) throws XmlPullParserException, IOException {\n            int type;\n            while ((type = parser.next()) != parser.START_TAG\n                    && type != parser.END_DOCUMENT) {\n            }\n\n            if (type != parser.START_TAG) {\n                throw new XmlPullParserException(\"No start tag found\");\n            }\n\n            if (!parser.getName().equals(firstElementName)) {\n                throw new XmlPullParserException(\"Unexpected start tag: found \" + parser.getName() +\n                        \", expected \" + firstElementName);\n            }\n        }\n\n        public static void nextElement(XmlPullParser parser) throws XmlPullParserException, IOException {\n            int type;\n            while ((type = parser.next()) != parser.START_TAG\n                    && type != parser.END_DOCUMENT) {\n            }\n        }\n    }\n}","sourceCodeStart":647,"sourceCodeEnd":677,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-battery-canary/src/main/java/com/tencent/matrix/batterycanary/utils/PowerProfile.java#L647-L677","documentation":"PowerProfile's XML parser throws XmlPullParserException(\"Unexpected start tag: found X, expected Y\") when the document's first element name does not match the expected firstElementName. It validates the root element so subsequent parsing assumptions hold, mirroring AOSP's PowerProfile implementation.","triggerScenarios":"Passing an XML whose root element is not the expected name (e.g. a generic <resources> or <device> root when \"powerprofile\" is expected) into the PowerProfile constructor.","commonSituations":"Using a power-profile XML copied from the wrong Android version with a renamed root; hand-written test fixtures with a mismatched root tag; pointing the parser at an arbitrary XML resource instead of a power profile.","solutions":["Make the XML root element match the expected name expected by the constructor (check the firstElementName argument you passed).","Pass the correct firstElementName to PowerProfile to match your document's root tag.","Use the library's bundled/default power-profile resource rather than a custom file unless intentionally overriding."],"exampleFix":"// before\nnew PowerProfile(parser, \"device\"); // XML root is <powerprofile>\n\n// after\nnew PowerProfile(parser, \"powerprofile\"); // matches XML root element","handlingStrategy":"validation","validationCode":"XmlPullParser p = res.getXml(R.xml.power_profile);\nint t;\nwhile ((t = p.next()) != XmlPullParser.START_TAG && t != XmlPullParser.END_DOCUMENT) {}\nif (t == XmlPullParser.START_TAG && !\"powerprofile\".equals(p.getName())) {\n    // wrong root tag: fix XML or pass matching firstElementName\n}","typeGuard":null,"tryCatchPattern":"try {\n    PowerProfile profile = new PowerProfile(parser, firstElementName);\n} catch (XmlPullParserException e) {\n    MatrixLog.e(TAG, \"bad power profile XML: \" + e.getMessage());\n}","preventionTips":["Match the constructor's firstElementName argument to the XML root element.","Copy power-profile XML from a matching Android version/source tree.","Never point the parser at unrelated XML resources."],"tags":["android","xml","parsing","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}