stanfordnlp/CoreNLP · error · SsurgeonParseException

XML failure while reading string

Error message

XML failure while reading string

What it means

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.

Solutions

  1. Validate the XML string with an XML parser/linter to find the malformed section
  2. Escape XML special characters (e.g. use &lt; for <, &amp; for &) inside rule text
  3. Read the chained exception for the exact parser error location

Example fix

// before
String xml = "<ssurgeon>... if a < b ...</ssurgeon>";
// after
String xml = "<ssurgeon>... if a &lt; b ...</ssurgeon>";
Defensive patterns

Strategy: try-catch

Validate before calling

// validate the string is well-formed XML first
try {
  javax.xml.parsers.DocumentBuilderFactory.newInstance()
    .newDocumentBuilder()
    .parse(new org.xml.sax.InputSource(new java.io.StringReader(text)));
  return true;
} catch (Exception e) { return false; }

Try / catch

try {
  List<SsurgeonPattern> pats = ssurgeon.readFromString(text);
} catch (SsurgeonParseException e) {
  log.severe("Malformed ssurgeon XML string: " + e.getCause());
}

Prevention

When it happens

Trigger: Calling Ssurgeon.readFromString(text) with malformed XML: unclosed tags, bad entities, wrong encoding declaration, or invalid characters.

Common situations: Embedding ssurgeon rules in a string with unescaped special characters (e.g. <, & in regexes), or hand-edited XML resources that broke well-formedness.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/25b7147e781e2ff2. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/semgraph/semgrex/ssurgeon/Ssurgeon.java:797

          editOrdinal++;
        }
        rootElt.appendChild(patElt);
        ordinal++;
      }
      return domDoc;
    } catch (Exception e) {
      log.error(Ssurgeon.class.getName(), "createPatternXML");
      log.error(e);
      return null;
    }
  }

  public List<SsurgeonPattern> readFromString(String text) {
    try {
      Document doc = XMLUtils.readDocumentFromString(text);
      return readFromDocument(doc);
    } catch (ParserConfigurationException | SAXException e) {
      throw new SsurgeonParseException("XML failure while reading string", e);
    }
  }

  /**
   * Given a path to a file containing a list of SsurgeonPatterns, returns
   *
   * TODO: deal with resources
   */
  public List<SsurgeonPattern> readFromFile(File file) {
    try {
      Document doc = XMLUtils.readDocumentFromFile(file.getPath());

      if (VERBOSE)
        System.out.println("Reading ssurgeon file="+file.getAbsolutePath());

      return readFromDocument(doc);
    } catch (ParserConfigurationException | SAXException e) {
      throw new SsurgeonParseException("XML failure while reading " + file, e);

View on GitHub (pinned to 1b7edd19c4)