{"record":{"id":"ccbe29865b20ae90","repo":"pentaho/pentaho-kettle","slug":"error-parsing-xml","errorCode":null,"errorMessage":"Error parsing XML","messagePattern":"Error parsing XML","errorType":"exception","errorClass":"KettleXMLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java","lineNumber":789,"sourceCode":"    if ( Boolean.TRUE.equals( deferNodeExpansion ) && ! Boolean.TRUE.equals( namespaceAware ) ) {\n      return loadXMLString( DEFAULT_BUILDER_CACHE.get(), string );\n    }\n    // For non-default configurations, create a one-off builder\n    DocumentBuilder db = createDocumentBuilder( namespaceAware, deferNodeExpansion );\n    return loadXMLString( db, string );\n  }\n\n  public static Document loadXMLString( DocumentBuilder db, String string ) throws KettleXMLException {\n\n    try {\n      StringReader stringReader = new java.io.StringReader( string );\n      InputSource inputSource = new InputSource( stringReader );\n\n      Document doc;\n      try {\n        doc = db.parse( inputSource );\n      } catch ( IOException ef ) {\n        throw new KettleXMLException( \"Error parsing XML\", ef );\n      } finally {\n        resetDocumentBuilder( db );\n        stringReader.close();\n      }\n\n      return doc;\n    } catch ( Exception e ) {\n      throw new KettleXMLException( \"Error reading information from XML string : \" + Const.CR + string, e );\n    }\n  }\n\n  public static DocumentBuilder createDocumentBuilder( boolean namespaceAware, boolean deferNodeExpansion )\n    throws KettleXMLException {\n    try {\n      DocumentBuilderFactory dbf = XMLParserFactoryProducer.createSecureDocBuilderFactory();\n      dbf.setFeature( \"http://apache.org/xml/features/dom/defer-node-expansion\", deferNodeExpansion );\n      dbf.setNamespaceAware( namespaceAware );\n      return dbf.newDocumentBuilder();","sourceCodeStart":771,"sourceCodeEnd":807,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java#L771-L807","documentation":"loadXMLString parses an XML string via the cached DocumentBuilder; when db.parse throws an IOException it is rethrown as KettleXMLException(\"Error parsing XML\", ef). This is the I/O branch of string parsing; general exceptions fall into the broader 'Error reading information from XML string' handler.","triggerScenarios":"XMLHandler.loadXMLString(String) where the Reader-backed InputSource throws IOException during parse (I/O failure while reading the in-memory string reader, or certain underlying parser I/O errors).","commonSituations":"Very rare in practice since input is an in-memory string; usually surfaces from low-level parser failures or JVM-level IO issues.","solutions":["Check e.getCause() for the underlying IOException.","Retry once after XMLHandler.resetDocumentBuilder() if the cached builder was in a bad state.","Validate the XML string syntactically before calling loadXMLString.","Ensure the string is complete and not truncated mid-document."],"exampleFix":"// before\nDocument doc = XMLHandler.loadXMLString(xmlFromSocket);\n// after\nif (xmlFromSocket == null || xmlFromSocket.trim().isEmpty()) {\n  throw new KettleException(\"Empty XML payload\");\n}\nDocument doc = XMLHandler.loadXMLString(xmlFromSocket);","handlingStrategy":"try-catch","validationCode":"if (xml == null || xml.trim().isEmpty()) throw new KettleException(\"Empty XML input\");","typeGuard":"boolean looksLikeXml(String s) {\n  return s != null && s.trim().startsWith(\"<\");\n}","tryCatchPattern":"try {\n  doc = XMLHandler.loadXMLString(xml);\n} catch (KettleXMLException e) {\n  log.error(\"Parse failed: \" + e.getCause());\n}","preventionTips":["Reject empty/truncated payloads before parsing","Reset the cached DocumentBuilder on repeated odd failures","Log the first 200 chars of failing input for diagnosis"],"tags":["xml","parsing","io"],"backgroundTag":"xml-parse-error","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}