{"record":{"id":"a302e0d996bd35c6","repo":"pentaho/pentaho-kettle","slug":"unable-to-format-node-as-xml","errorCode":null,"errorMessage":"Unable to format Node as XML","messagePattern":"Unable to format Node as XML","errorType":"exception","errorClass":"KettleXMLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java","lineNumber":1283,"sourceCode":"  }\n\n  public static String closeTag( String tag ) {\n    return closeTag( new StringBuilder(), tag ).toString();\n  }\n\n  public static StringBuilder closeTag( StringBuilder builder, String tag ) {\n    return builder.append( \"</\" ).append( tag ).append( '>' );\n  }\n\n  public static String formatNode( Node node ) throws KettleXMLException {\n    StringWriter sw = new StringWriter();\n    try {\n\n      Transformer t = XMLParserFactoryProducer.createSecureTransformerFactory().newTransformer();\n      t.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, \"yes\" );\n      t.transform( new DOMSource( node ), new StreamResult( sw ) );\n    } catch ( Exception e ) {\n      throw new KettleXMLException( \"Unable to format Node as XML\", e );\n    }\n    return sw.toString();\n  }\n\n  /**\n   * <p>Checks if a given {@link FileObject} instance corresponds to an existing file.</p>\n   *\n   * @param fileObject the {@link FileObject} instance to check\n   * @return <code>true</code> if the file exists, <code>false</code> otherwise\n   * @throws KettleXMLException if an error occurred while checking\n   */\n  public static boolean checkFile( FileObject fileObject ) throws KettleXMLException {\n    try {\n      return fileObject != null && fileObject.exists() && fileObject.isFile();\n    } catch ( FileSystemException e ) {\n      throw new KettleXMLException( BaseMessages.getString(\n        PKG, \"XMLHandler.errorCheckingFileExistence\", fileObject.toString() ), e );\n    }","sourceCodeStart":1265,"sourceCodeEnd":1301,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/xml/XMLHandler.java#L1265-L1301","documentation":"Node2String serializes a DOM Node back to an XML string using a Transformer from XMLParserFactoryProducer.createSecureTransformerFactory(). Any TransformerConfigurationException or transformation failure (TransformerException wrapped as Exception) is rethrown as KettleXMLException(\"Unable to format Node as XML\").","triggerScenarios":"XMLHandler.Node2String(Node) where newTransformer() fails (secure factory/transformer feature misconfiguration) or t.transform() fails (DOM contains nodes the transformer cannot serialize, or the node tree is inconsistent).","commonSituations":"Custom Xalan/Xerces versions on the classpath conflicting with the secure factory, JVM system properties forcing an incompatible TransformerFactory, nodes built by a different DOM implementation (namespace-incompatible trees).","solutions":["Check e.getCause(): TransformerConfigurationException points to factory problems; fix the -Djavax.xml.transform.TransformerFactory system property or remove conflicting Xalan jars.","Ensure the Node was created by a document builder compatible with the transformer (same DOM implementation).","Update to a consistent parser/transformer stack (bundled JAXP versions).","If only debugging, log the node with a simpler serializer or rebuild the node via XMLHandler.loadXMLString/XMLHandler.createDocumentBuilder."],"exampleFix":"// before\nString xml = XMLHandler.Node2String(node); // fails with custom TransformerFactory\n// after\nString xml;\ntry {\n  xml = XMLHandler.Node2String(node);\n} catch (KettleXMLException e) {\n  log.warn(\"Node2String failed: \" + e.getCause());\n  xml = node.toString(); // or fix factory config\n}","handlingStrategy":"try-catch","validationCode":"if (node == null || node.getNodeType() != Node.ELEMENT_NODE && node.getNodeType() != Node.DOCUMENT_NODE) {\n  throw new KettleException(\"Not a serializable node\");\n}","typeGuard":"boolean isSerializableNode(Node n) {\n  return n != null && (n.getNodeType() == Node.ELEMENT_NODE\n    || n.getNodeType() == Node.DOCUMENT_NODE);\n}","tryCatchPattern":"try {\n  xml = XMLHandler.Node2String(node);\n} catch (KettleXMLException e) {\n  log.error(\"Serialize failed: \" + e.getCause()); // TransformerException\n}","preventionTips":["Remove conflicting Xalan/Xerces jars so the secure factory loads","Build nodes with the same DOM implementation you serialize","Pin javax.xml.transform.TransformerFactory system property explicitly"],"tags":["xml","serialization","dom"],"backgroundTag":"json-serialization-failed","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"}