public static String getIndented(String inXml) throws IOException { try { final InputSource src = new InputSource(new StringReader(inXml)); final Document domDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src); String encoding = domDoc.getXmlEncoding(); if (encoding == null) { // defaults to UTF-8 encoding = "UTF-8"; } final Node document = domDoc.getDocumentElement(); final boolean keepDeclaration = inXml.startsWith("<?xml"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); writer.setNewLine("\n"); writer.getDomConfig().setParameter("format-pretty-print", true); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted. LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding(encoding); Writer stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); writer.write(document, lsOutput); return stringWriter.toString(); } catch (ParserConfigurationException | SAXException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) { throw new XMLException(null, ex); } }
output.setCharacterStream(result); LSSerializer serializer = lsImpl.createLSSerializer(); serializer.setNewLine("\n"); output.setEncoding(doc.getXmlEncoding()); serializer.write(doc, output);
/** * Creates a new XMLSerializer object. * @param doc Document to be serialized */ public XMLSerializer(Document doc) { // Store the ref this.doc = doc; // Create the LSSerializer and LSOutput instances DOMImplementationRegistry registry; try { registry = DOMImplementationRegistry.newInstance(); } catch (Exception e) { throw new RuntimeException("Cannot get the system DOMImplementationRegistry", e); } DOMImplementation impl = registry.getDOMImplementation("LS 3.0"); DOMImplementationLS factory = (DOMImplementationLS) impl; // Define the format for the xml document serializer = factory.createLSSerializer(); serializer.setNewLine(DEF_LINE_SEP); output = factory.createLSOutput(); output.setEncoding("UTF-8"); //DOMConfiguration dc = serializer.getDomConfig(); //dc.setParameter("format-pretty-print", Boolean.TRUE); //dc.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); }
/** * Creates a new XMLSerializer object. * @param node Node to be serialized */ public XMLSerializer(final Node node) { // Store the ref this.node = node; // Create the LSSerializer and LSOutput instances DOMImplementationRegistry registry; try { registry = DOMImplementationRegistry.newInstance(); } catch (Exception e) { throw new RuntimeException("Cannot get the system DOMImplementationRegistry", e); } DOMImplementation impl = registry.getDOMImplementation("LS 3.0"); DOMImplementationLS factory = (DOMImplementationLS) impl; // Define the format for the xml document serializer = factory.createLSSerializer(); serializer.setNewLine(DEF_LINE_SEP); output = factory.createLSOutput(); output.setEncoding("UTF-8"); // DOMConfiguration dc = serializer.getDomConfig(); // dc.setParameter("format-pretty-print", Boolean.TRUE); // dc.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); }
/** * Creates a new XMLSerializer object. * @param node Node to be serialized */ public XMLSerializer(final Node node) { // Store the ref this.node = node; // Create the LSSerializer and LSOutput instances DOMImplementationRegistry registry; try { registry = DOMImplementationRegistry.newInstance(); } catch (Exception e) { throw new RuntimeException("Cannot get the system DOMImplementationRegistry", e); } DOMImplementation impl = registry.getDOMImplementation("LS 3.0"); DOMImplementationLS factory = (DOMImplementationLS) impl; // Define the format for the xml document serializer = factory.createLSSerializer(); serializer.setNewLine(DEF_LINE_SEP); output = factory.createLSOutput(); output.setEncoding("UTF-8"); // DOMConfiguration dc = serializer.getDomConfig(); // dc.setParameter("format-pretty-print", Boolean.TRUE); // dc.setParameter("schema-type", "http://www.w3.org/2001/XMLSchema"); }
LSSerializer serializer = LS_IMPL.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", withXmlDeclaration); serializer.setNewLine("\n"); String encoding = "UTF-8"; if (node instanceof Document) {
LSSerializer serializer = this.lsImpl.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", withXmlDeclaration); serializer.setNewLine("\n"); String encoding = "UTF-8"; if (node instanceof Document) {
domConfig.setParameter("format-pretty-print", true); domConfig.setParameter("element-content-whitespace", true); domWriter.setNewLine("\r\n"); domConfig.setParameter("cdata-sections", Boolean.TRUE);