private static String prettyFormat(Document document, boolean keepXmlDeclaration) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS"); Writer stringWriter = new StringWriter(); LSOutput formattedOutput = domImplementation.createLSOutput(); formattedOutput.setCharacterStream(stringWriter); LSSerializer domSerializer = domImplementation.createLSSerializer(); domSerializer.getDomConfig().setParameter("format-pretty-print", true); // Set this to true if the declaration is needed to be in the output. domSerializer.getDomConfig().setParameter("xml-declaration", keepXmlDeclaration); domSerializer.write(document, formattedOutput); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(FORMAT_ERROR, e); } }
private static String prettyFormat(Document document, boolean keepXmlDeclaration) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS"); Writer stringWriter = new StringWriter(); LSOutput formattedOutput = domImplementation.createLSOutput(); formattedOutput.setCharacterStream(stringWriter); LSSerializer domSerializer = domImplementation.createLSSerializer(); domSerializer.getDomConfig().setParameter("format-pretty-print", true); // Set this to true if the declaration is needed to be in the output. domSerializer.getDomConfig().setParameter("xml-declaration", keepXmlDeclaration); domSerializer.write(document, formattedOutput); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(FORMAT_ERROR, e); } }
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); } }
private static String prettyPrintXmlDocument(Document document) { // Pretty-prints a DOM document to XML using DOM Load and Save's LSSerializer. // Note that the "format-pretty-print" DOM configuration parameter can only be set in JDK 1.6+. DOMImplementation domImplementation = document.getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) { lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); } else { throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable."); } } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
/** * Creates a serializer using the given character stream and encoding. * * @param pStream the characted stream. * @throws IllegalStateException if no {@code DOMImplementation} with the right features can be instantiated. */ public DOMSerializer(final Writer pStream) { this(); output.setCharacterStream(pStream); }
public static String serialize(Document document, boolean prettyPrint) { DOMImplementationLS impl = getDOMImpl(); LSSerializer serializer = impl.createLSSerializer(); // document.normalizeDocument(); DOMConfiguration config = serializer.getDomConfig(); if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) { config.setParameter("format-pretty-print", true); } config.setParameter("xml-declaration", true); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); Writer writer = new StringWriter(); output.setCharacterStream(writer); serializer.write(document, output); return writer.toString(); }
private byte[] getByteArray(Document config) { DOMImplementationLS impl = (DOMImplementationLS) config.getImplementation(); LSSerializer serializer = impl.createLSSerializer(); LSOutput out = impl.createLSOutput(); out.setEncoding("UTF-8"); Writer stringWriter = new StringWriter(); out.setCharacterStream(stringWriter); serializer.write(config, out); return stringWriter.toString().getBytes(); } }
private String getString(Document config) { DOMImplementationLS impl = (DOMImplementationLS) config.getImplementation(); LSSerializer serializer = impl.createLSSerializer(); LSOutput out = impl.createLSOutput(); out.setEncoding("UTF-8"); Writer stringWriter = new StringWriter(); out.setCharacterStream(stringWriter); serializer.write(config, out); return stringWriter.toString(); }
/** * Creates a serializer using the given character stream and encoding. * * @param pStream the characted stream. * @throws IllegalStateException if no {@code DOMImplementation} with the right features can be instantiated. */ public DOMSerializer(final Writer pStream) { this(); output.setCharacterStream(pStream); }
/** * Creates a serializer using the given character stream and encoding. * * @param pStream the characted stream. * @throws IllegalStateException if no {@code DOMImplementation} with the right features can be instantiated. */ public DOMSerializer(final Writer pStream) { this(); mOutput.setCharacterStream(pStream); }
DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry. DOMImplementationLS domImplementationLS = (DOMImplementationLS)REGISTRY.getDOMImplementation("LS"); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); Writer stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(doc, lsOutput); String result = stringWriter.toString();
/** * Serialize the encapsulated Document into the Writer * * @param writer writer * * @throws IOException When serialization fails */ public void serialize(Writer writer) throws IOException { output.setCharacterStream(writer); serialize(); }
/** * Serialize the encapsulated Node into the Writer. * @param writer writer * @throws IOException When serialization fails */ public void serialize(final Writer writer) throws IOException { output.setCharacterStream(writer); serialize(); }
/** * Serialize the encapsulated Node into the Writer. * @param writer writer * @throws IOException When serialization fails */ public void serialize(final Writer writer) throws IOException { output.setCharacterStream(writer); serialize(); }
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse("/path/to/file.xml"); DOMImplementation domImpl = ownerDocument.getImplementation(); DOMImplementationLS domImplLS = (DOMImplementationLS)domImpl.getFeature("LS", "3.0"); LSSerializer serializer = domImplLS.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", Boolean.valueOf(false)); LSOutput lsOutput = domImplLS.createLSOutput(); lsOutput.setCharacterStream(output); serializer.write(doc, lsOutput);
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); LSOutput lsOutput = domImplementation.createLSOutput( ); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter=new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(doc,lsOutput); return stringWriter.toString();
public static void serialize(final Element value, final JsonWriter sw) { Document document = value.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); LSOutput lsOutput = domImplLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter writer = new StringWriter(); lsOutput.setCharacterStream(writer); serializer.write(document, lsOutput); StringConverter.serialize(writer.toString(), sw); }
@Override public String prettyPrint(String xml) throws Exception { final Document document = newDocument(xml, "UTF-8"); final DOMImplementationLS domImplementationLS = getDomImplementationLS(document); final LSSerializer lsSerializer = newLSSerializer(domImplementationLS); final LSOutput lsOutput = newLSOutput(domImplementationLS); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); }
@Override public String prettyPrint(String xml) throws Exception { final Document document = newDocument(xml, "UTF-8"); final DOMImplementationLS domImplementationLS = getDomImplementationLS(document); final LSSerializer lsSerializer = newLSSerializer(domImplementationLS); final LSOutput lsOutput = newLSOutput(domImplementationLS); final StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); }
public void serialize(Writer w, Node node, Format format) throws XMLException { try { LSSerializer ser = createLSSerializer(format); LSOutput out = domImplementationLS.createLSOutput(); out.setCharacterStream(w); ser.write(node,out); } catch(Exception e) { throw new XMLException(e,"Error while converting XML document to string"); // $NLS-AbstractXercesDriver.ErrorwhileconvertingXMLdocumentto.1-1$ } }