OtterMind/Chat2DB · error · RuntimeException

Error parsing XML document

Error message

Error parsing XML document

What it means

RuntimeException 'Error parsing XML document' from XMLUtils.parseDocument(InputSource) when DocumentBuilder.parse fails — SAXParseException (malformed XML), ParserConfigurationException, or an I/O error mid-read. The factory is hardened with secure processing and doctype disallowal, so a DOCTYPE in the input will also land here.

Source

Thrown at chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/ncx/XMLUtils.java:54

    public static Document parseDocument(InputStream is) {
        return parseDocument(new InputSource(is));
    }

    public static Document parseDocument(java.io.Reader is) {
        return parseDocument(new InputSource(is));
    }

    public static Document parseDocument(InputSource source) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
            dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            DocumentBuilder xmlBuilder = dbf.newDocumentBuilder();
            return xmlBuilder.parse(source);
        } catch (Exception er) {
            throw new RuntimeException("Error parsing XML document", er);
        }
    }

    public static Document createDocument() {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder xmlBuilder = dbf.newDocumentBuilder();
            return xmlBuilder.newDocument();
        } catch (Exception er) {
            throw new RuntimeException("Error creating XML document", er);
        }
    }

    public static Element getChildElement(Element element,  String childName) {
        if (element == null) {
            return null;
        }
        for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {

View on GitHub (pinned to 5ee1e990e7)

Solutions

  1. Open the file in an XML validator and fix well-formedness errors.
  2. Remove any DOCTYPE declaration; the parser explicitly disallows it for security.
  3. Re-export the file from its source tool if it is malformed.
  4. Check encoding matches the XML declaration.
Defensive patterns

Strategy: try-catch

Validate before calling

// strip DOCTYPE and validate well-formedness before parse
String content = java.nio.file.Files.readString(java.nio.file.Path.of(fileName));
if (content.contains("<!DOCTYPE")) throw new IllegalArgumentException("DOCTYPE not allowed");

Try / catch

try { return XMLUtils.parseDocument(inputSource); }
catch (RuntimeException e) {
    // message is generic 'Error parsing XML document'; inspect cause for SAXParseException line/col
    throw new ConfigImportException("malformed config file: " + e.getCause().getMessage(), e);
}

Prevention

When it happens

Trigger: Parsing an NCX/DBP/JSON-import XML file that is not well-formed, has an unexpected encoding, or contains a DOCTYPE/external entity (blocked by FEATURE_SECURE_PROCESSING + disallow-doctype-decl).

Common situations: Corrupt or truncated export file; file saved with a different encoding; file authored with an XML editor that inserts a DOCTYPE; XXE attempt blocked by secure processing.

Related errors


AI-assisted analysis of OtterMind/Chat2DB@5ee1e990e7 (2026-08-14). Data as JSON: /api/errors/c0d2ec532ef5c87c. Report an issue: GitHub.