{"record":{"id":"212866bf8140c234","repo":"pentaho/pentaho-kettle","slug":"error-reading-information-from-xml-string","errorCode":null,"errorMessage":"Error reading information from XML string : ","messagePattern":"Error reading information from XML string : ","errorType":"exception","errorClass":"KettleXMLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java","lineNumber":797,"sourceCode":"  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();\n    } catch ( ParserConfigurationException e ) {\n      throw new KettleXMLException( e );\n    }\n  }\n\n  private static void resetDocumentBuilder( DocumentBuilder db ) {\n    // ThreadLocal-cached builders are reused, so reset parser state between invocations.\n    try {","sourceCodeStart":779,"sourceCodeEnd":815,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java#L779-L815","documentation":"loadXMLString wraps any exception (SAXParseException, etc.) from parsing an XML string into KettleXMLException(\"Error reading information from XML string : \" + Const.CR + string), appending the offending XML to the message. This is the general failure path for string-to-DOM conversion.","triggerScenarios":"XMLHandler.loadXMLString(String) with syntactically invalid XML: unclosed tags, illegal characters, multiple root elements, invalid entities, empty or null-formatted input.","commonSituations":"Capturing server responses and feeding them to the parser, truncated XML from socket reads, XML containing unescaped '&' or '<', transformation metadata stored as broken XML in a database.","solutions":["Examine the embedded string in the message around the reported error position; run it through xmllint to find the syntax fault.","Escape XML special characters in dynamically built XML (use XMLHandler.buildCDATA or escapeHtml-style escaping).","Verify the source actually returned XML and not an HTML error page or empty body.","Fix encoding issues by ensuring the string was decoded with the correct charset."],"exampleFix":"// before\nString xml = \"<root><a>\" + userInput + \"</a></root>\";\nDocument doc = XMLHandler.loadXMLString(xml);\n// after\nString safe = \"<root><a><![CDATA[\" + userInput.replace(\"]]>\", \"]]]]><![CDATA[>\") + \"]]></a></root>\";\nDocument doc = XMLHandler.loadXMLString(safe);","handlingStrategy":"try-catch","validationCode":"DocumentBuilderFactory.newInstance().newDocumentBuilder()\n  .parse(new InputSource(new StringReader(xml))); // pre-validate","typeGuard":"boolean isWellFormedXml(String s) {\n  try {\n    javax.xml.parsers.DocumentBuilderFactory.newInstance()\n      .newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(s)));\n    return true;\n  } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n  doc = XMLHandler.loadXMLString(xml);\n} catch (KettleXMLException e) {\n  // message embeds the offending string\n  log.error(\"Bad XML: \" + e.getMessage(), e);\n}","preventionTips":["Escape or CDATA-wrap dynamic content before building XML strings","Confirm sources return XML, not HTML error pages","Decode bytes with the correct charset before parsing"],"tags":["xml","parsing","malformed-input"],"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"}