List of usage examples for org.w3c.dom.ls LSOutput setEncoding
public void setEncoding(String encoding);
From source file:Main.java
public static void main(String[] args) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0"); if (impl == null) { System.out.println("No DOMImplementation found !"); System.exit(0);// w w w . java 2s . co m } System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName()); LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml"); // http://www.w3.org/2001/XMLSchema System.out.printf("LSParser: %s\n", parser.getClass().getName()); Document doc = parser.parseURI(""); LSSerializer serializer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(System.out); serializer.write(doc, output); System.out.println(); }
From source file:Main.java
public static String elementToString(Element element) { Document document = element.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); // Client assumes/requires UTF-8 response. LSOutput lsOutput = domImplLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(element, lsOutput); return stringWriter.toString(); }
From source file:Main.java
public static final String prettyPrint(final Document aNode) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS"); // Prepare the output LSOutput domOutput = impls.createLSOutput(); domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name()); StringWriter writer = new StringWriter(); domOutput.setCharacterStream(writer); LSSerializer domWriter = impls.createLSSerializer(); DOMConfiguration domConfig = domWriter.getDomConfig(); domConfig.setParameter("format-pretty-print", true); domConfig.setParameter("element-content-whitespace", true); domWriter.setNewLine("\r\n"); domConfig.setParameter("cdata-sections", Boolean.TRUE); // And finaly, write domWriter.write(aNode, domOutput);//w w w. ja v a 2 s .co m return domOutput.getCharacterStream().toString(); }
From source file:Main.java
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); }//from w w w . ja va 2 s . c om 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(); }
From source file:Main.java
public static String lsSerializePretty(Document doc) { DOMImplementation domImplementation = doc.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(doc, lsOutput); return stringWriter.toString(); } else {//from w w w . j a v a 2s . co m 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."); } }
From source file:Main.java
public static String prettyPrintWithDOM3LS(Document document) { 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 {//from w w w. j a v a 2 s. c o m 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."); } }
From source file:Main.java
public static void writeDocumentToDisk(Document document, File directoryToWriteTo, String fileName) { 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"); try { lsOutput.setByteStream(new FileOutputStream(new File(directoryToWriteTo, fileName))); } catch (FileNotFoundException e) { throw new IllegalStateException(e); }//from www . j a va 2 s . c o m lsSerializer.write(document, lsOutput); } } }
From source file:Main.java
public static void writeNode(Node node, OutputStream os, boolean formatted) throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException { DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry .getDOMImplementation("LS"); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); lsOutput.setByteStream(os);// w w w. j a v a 2s .c o m LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); if (formatted) setLsSeriliserToFormatted(lsSerializer); lsSerializer.write(node, lsOutput); }
From source file:Main.java
public static void writeDocument(Document doc, Writer out) throws IOException { // happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer // approach to work. // OutputFormat format = new OutputFormat(doc); // format.setIndenting(true); // format.setIndent(2); // XMLSerializer serializer = new XMLSerializer(out, format); // serializer.serialize(doc); DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation(); LSSerializer writer = impl.createLSSerializer(); DOMConfiguration config = writer.getDomConfig(); if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) { config.setParameter("format-pretty-print", Boolean.TRUE); }//from w w w . j av a 2s . co m // what a crappy way to force the stream to be UTF-8. yuck! ByteArrayOutputStream baos = new ByteArrayOutputStream(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(baos); writer.write(doc, output); out.write(baos.toString()); out.flush(); }
From source file:Main.java
/** * Serialize XML Node to string/*w w w .j av a 2 s . c o m*/ * <p> * Note: this method is supposed to be faster than the Transform version but the output control * is limited. If node is Document node, it will output XML PI which sometimes we want to avoid. * * @param doc XML document * @param node Node to be serialized * @param encoding encoding for the output * @return String representation of the Document * @throws IOException */ public static String serializeToStringLS(Document doc, Node node, String encoding) throws IOException { DOMImplementationLS domImpl = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImpl.createLSSerializer(); LSOutput output = domImpl.createLSOutput(); output.setEncoding(encoding); StringWriter writer = new StringWriter(); output.setCharacterStream(writer); lsSerializer.write(node, output); writer.flush(); return writer.toString(); }