{"record":{"id":"4f0293dbe3b3fe1f","repo":"Activiti/Activiti","slug":"misshaped-element","errorCode":null,"errorMessage":"Misshaped element","messagePattern":"Misshaped element","errorType":"exception","errorClass":"JSONException","httpStatus":null,"severity":"error","filePath":"activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/XMLTokener.java","lineNumber":241,"sourceCode":"\n    /**\n     * Get the next XML Token. These tokens are found inside of angle brackets. It may be one of these characters: <code>/ > = ! ?</code> or it may be a string wrapped in single quotes or double quotes,\n     * or it may be a name.\n     *\n     * @return a String or a Character.\n     * @throws JSONException\n     *           If the XML is not well formed.\n     */\n    public Object nextToken() throws JSONException {\n        char c;\n        char q;\n        StringBuffer sb;\n        do {\n            c = next();\n        } while (Character.isWhitespace(c));\n        switch (c) {\n            case 0:\n                throw syntaxError(\"Misshaped element\");\n            case '<':\n                throw syntaxError(\"Misplaced '<'\");\n            case '>':\n                return XML.GT;\n            case '/':\n                return XML.SLASH;\n            case '=':\n                return XML.EQ;\n            case '!':\n                return XML.BANG;\n            case '?':\n                return XML.QUEST;\n            // Quoted string\n\n            case '\"':\n            case '\\'':\n                q = c;\n                sb = new StringBuffer();","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/Activiti/Activiti/blob/56435b1a97deeafdc09dd40074b056c89fba5a8a/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/util/json/XMLTokener.java#L223-L259","documentation":"Thrown by XMLTokener.nextToken() when it reaches end-of-input while looking for the next token inside an element's angle brackets. After '<' opens a tag, the parser needs at least a name and a closing '>' but the document ends first. Equivalent to an unclosed tag at end of input.","triggerScenarios":"XML.toJSONObject / JSONML.toJSONObject on text like '<foo' or '<a href=\"x\"' — an opening '<' with no '>' before the string ends, often from truncated responses or files.","commonSituations":"Partial downloads of XML files; SAX/stream readers that pass partial buffers; logs where the trailing '>' was clipped; pipeline tools writing XML without final flush.","solutions":["Ensure the XML string contains the complete document including all closing '>' characters","Check that all stream/file reads consume the entire document before parsing","Run the input through a strict XML parser (DocumentBuilder) to detect truncation with a clearer message","If consuming incrementally, accumulate until the root element is closed before converting"],"exampleFix":"// before\nString xml = \"<process id=\\\"p1\\\"\"; // tag never closed\nXML.toJSONObject(xml); // throws\n// after\nString xml = \"<process id=\\\"p1\\\"/>\";\nXML.toJSONObject(xml);","handlingStrategy":"validation","validationCode":"public static boolean isCompleteDocument(String xml) {\n    String s = xml == null ? \"\" : xml.trim();\n    return s.startsWith(\"<\") && s.endsWith(\">\")\n        && javax.xml.XMLConstants.class != null; // plus a strict check below\n}\n// stronger: try { DocumentBuilderFactory.newInstance().newDocumentBuilder()\n//   .parse(new InputSource(new StringReader(xml))); return true; } catch (...) { return false; }","typeGuard":null,"tryCatchPattern":"try {\n    JSONObject obj = XML.toJSONObject(xml);\n} catch (JSONException e) {\n    if (e.getMessage().contains(\"Misshaped element\")) {\n        throw new IllegalArgumentException(\"XML input is truncated: a tag was left unclosed\", e);\n    }\n    throw e;\n}","preventionTips":["Read files/streams fully (loop until EOF) before parsing, don't pass partial buffers","Check that streamed downloads match the expected byte count","Log the tail of the input string when parse errors occur to spot truncation","Detect truncation with DocumentBuilder before using the JSON converter"],"tags":["xml","parsing","jsonexception","truncated-input"],"backgroundTag":"invalid-argument-format","analyzedSha":"56435b1a97deeafdc09dd40074b056c89fba5a8a","analyzedAt":"2026-09-09T21:00:06.703Z","contentChangedAt":"2026-09-09T21:00:06.703Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}