{"record":{"id":"25b7147e781e2ff2","repo":"stanfordnlp/CoreNLP","slug":"xml-failure-while-reading-string","errorCode":null,"errorMessage":"XML failure while reading string","messagePattern":"XML failure while reading string","errorType":"exception","errorClass":"SsurgeonParseException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/Ssurgeon.java","lineNumber":797,"sourceCode":"          editOrdinal++;\n        }\n        rootElt.appendChild(patElt);\n        ordinal++;\n      }\n      return domDoc;\n    } catch (Exception e) {\n      log.error(Ssurgeon.class.getName(), \"createPatternXML\");\n      log.error(e);\n      return null;\n    }\n  }\n\n  public List<SsurgeonPattern> readFromString(String text) {\n    try {\n      Document doc = XMLUtils.readDocumentFromString(text);\n      return readFromDocument(doc);\n    } catch (ParserConfigurationException | SAXException e) {\n      throw new SsurgeonParseException(\"XML failure while reading string\", e);\n    }\n  }\n\n  /**\n   * Given a path to a file containing a list of SsurgeonPatterns, returns\n   *\n   * TODO: deal with resources\n   */\n  public List<SsurgeonPattern> readFromFile(File file) {\n    try {\n      Document doc = XMLUtils.readDocumentFromFile(file.getPath());\n\n      if (VERBOSE)\n        System.out.println(\"Reading ssurgeon file=\"+file.getAbsolutePath());\n\n      return readFromDocument(doc);\n    } catch (ParserConfigurationException | SAXException e) {\n      throw new SsurgeonParseException(\"XML failure while reading \" + file, e);","sourceCodeStart":779,"sourceCodeEnd":815,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/Ssurgeon.java#L779-L815","documentation":"readFromString parses an ssurgeon XML document from a String via XMLUtils.readDocumentFromString. If the XML parser cannot be configured or the text is not well-formed XML (SAXException), it wraps the failure in SsurgeonParseException with this message.","triggerScenarios":"Calling Ssurgeon.readFromString(text) with malformed XML: unclosed tags, bad entities, wrong encoding declaration, or invalid characters.","commonSituations":"Embedding ssurgeon rules in a string with unescaped special characters (e.g. <, & in regexes), or hand-edited XML resources that broke well-formedness.","solutions":["Validate the XML string with an XML parser/linter to find the malformed section","Escape XML special characters (e.g. use &lt; for <, &amp; for &) inside rule text","Read the chained exception for the exact parser error location"],"exampleFix":"// before\nString xml = \"<ssurgeon>... if a < b ...</ssurgeon>\";\n// after\nString xml = \"<ssurgeon>... if a &lt; b ...</ssurgeon>\";","handlingStrategy":"try-catch","validationCode":"// validate the string is well-formed XML first\ntry {\n  javax.xml.parsers.DocumentBuilderFactory.newInstance()\n    .newDocumentBuilder()\n    .parse(new org.xml.sax.InputSource(new java.io.StringReader(text)));\n  return true;\n} catch (Exception e) { return false; }","typeGuard":null,"tryCatchPattern":"try {\n  List<SsurgeonPattern> pats = ssurgeon.readFromString(text);\n} catch (SsurgeonParseException e) {\n  log.severe(\"Malformed ssurgeon XML string: \" + e.getCause());\n}","preventionTips":["Escape &, <, > in regex/text inside the XML","Validate XML strings with a parser before passing to readFromString","Store rules as files validated in CI rather than inline strings"],"tags":["java","xml","ssurgeon","parse-error"],"backgroundTag":"xml-parse-error","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}