I assume that you want to serialize the document in order to pass it to a test file. To do this, you need to transfer your document to an empty XSL transformer, for example:
Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = result.getWriter().toString(); System.out.println(xmlString);
See also: How to print XML well with Java?
prote source share