Document document = domImpl.createDocument("urn:example.namespace", "my:example", null); Element element = document.createElementNS("http://another.namespace", "element"); document.getDocumentElement().appendChild(element); return document; LSSerializer lss = ls.createLSSerializer(); LSOutput lso = ls.createLSOutput(); lso.setByteStream(System.out); lss.write(document, lso);
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."); } }
fEncoding = destination.getEncoding(); if (fEncoding == null ) { fEncoding = getInputEncoding(nodeArg); Writer writer = destination.getCharacterStream(); if (writer == null ) { OutputStream outputStream = destination.getByteStream(); if ( outputStream == null) { String uri = destination.getSystemId(); if (uri == null) { String msg = Utils.messages
LSParser parser = impl.createLSParser( DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml"); LSSerializer serializer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(System.out); serializer.write(doc, output); System.out.println();
DOMImplementationLS ls = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0"); // NOI18N assert ls != null : "No DOM 3 LS supported in " + doc.getClass().getName(); LSOutput output = ls.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(baos); LSSerializer ser = ls.createLSSerializer(); String fpp = "format-pretty-print"; // NOI18N if (ser.getDomConfig().canSetParameter(fpp, true)) { ser.getDomConfig().setParameter(fpp, true); ser.write(doc, output); byte[] data = baos.toByteArray(); XMLUtil.parse(new InputSource(new ByteArrayInputStream(data)), true, true, ERROR_HANDLER, ENTITY_RESOLVER);
public static String writeToString(Node node) throws IOException { LSSerializer xmlSerializer = getSerializer(); StringWriter stringWriter = new StringWriter(); LSOutput destination = DOM_IMPLEMENTATION.createLSOutput(); destination.setCharacterStream(stringWriter); try { xmlSerializer.write(node, destination); } catch (LSException exception) { throw new IOException("Error while writing document to output stream.", exception); } return stringWriter.toString(); }
import java.io.StringWriter; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.ls.*; public class DomIO { public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .newDocument(); doc.appendChild(doc.createElement("foo")); System.out.println(getDocumentString(doc)); } public static String getDocumentString(Document doc) { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); LSOutput lsOut = domImplementation.createLSOutput(); lsOut.setEncoding("UTF-8"); lsOut.setCharacterStream(new StringWriter()); lsSerializer.write(doc, lsOut); return lsOut.getCharacterStream().toString(); } }
StringWriter stringWriter=new StringWriter(); try { LSOutput domOutput = impls.createLSOutput(); domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name()); domOutput.setCharacterStream(stringWriter); domOutput.setEncoding(ENCODING); 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); domWriter.write(source, domOutput); subscrXML = domOutput.getCharacterStream().toString(); DOMStringList dsl=domConfig.getParameterNames(); System.out.println(subscrXML);
/** * Prints the dom node. * * @param node * the node */ private void printDomNode(final Node node) { final Writer out = new StringWriter(); final LSSerializer writer = this.domImpl.createLSSerializer(); final LSOutput output = this.domImpl.createLSOutput(); output.setCharacterStream(out); output.setEncoding(UTF_8); writer.getDomConfig().setParameter(FORMAT_PRETTY_PRINT, Boolean.TRUE); writer.write(node, output); final String str = output.getCharacterStream().toString(); log.debug(str); }
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();
dom.getDocumentElement().removeAttribute("resultType"); DOMImplementationLS domImpl = (DOMImplementationLS) dom.getImplementation(); // safe cast as LSOutput destination = domImpl.createLSOutput(); destination.setByteStream(out); LSSerializer serializer = domImpl.createLSSerializer(); DOMConfiguration domConfig = serializer.getDomConfig(); if (domConfig.canSetParameter("format-pretty-print", Boolean.TRUE)) { domConfig.setParameter("format-pretty-print", Boolean.TRUE); serializer.write(dom, destination);
public static InputStream getInputStream(Document doc) throws Exception { DOMImplementationLS impl = null; DOMImplementation docImpl = doc.getImplementation(); // Try to get the DOMImplementation from doc first before // defaulting to the sun implementation. if (docImpl != null && docImpl.hasFeature("LS", "3.0")) { impl = (DOMImplementationLS)docImpl.getFeature("LS", "3.0"); } else { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); if (impl == null) { System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS)registry.getDOMImplementation("LS"); } } LSOutput output = impl.createLSOutput(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); output.setByteStream(byteArrayOutputStream); LSSerializer writer = impl.createLSSerializer(); writer.write(doc, output); byte[] buf = byteArrayOutputStream.toByteArray(); return new ByteArrayInputStream(buf); }
/** * {@inheritDoc} */ public void sendToWriter(Writer writer) throws IOException { String charset = mimeType.getParameter("charset"); try { DOMImplementationLS domImpl = (DOMImplementationLS) ((Document) domNode).getImplementation().getFeature("LS", "3.0"); LSOutput output = domImpl.createLSOutput(); if (charset != null) { output.setEncoding(charset); } else { output.setEncoding(java.nio.charset.Charset.defaultCharset().name()); } output.setCharacterStream(writer); LSSerializer serial = domImpl.createLSSerializer(); serial.write(domNode, output); } catch (Throwable ex) { if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else if (ex instanceof Error) { throw (Error) ex; } else { throw new UndeclaredThrowableException(ex); } } }
/** * Convert node into a string using the Document Object Model (DOM) Level 3 * Load and Save Implementation * @param node Node * @param writer Writer */ public static final void writeNodeLS( final Node node, final Writer writer ) { init(); final LSOutput output = domImplLs.createLSOutput(); output.setCharacterStream( writer ); final LSSerializer serializer = domImplLs.createLSSerializer(); serializer.write( node, output ); }
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 ls = (DOMImplementationLS)document.getImplementation(); LSOutput output = ls.createLSOutput(); LSSerializer ser = ls.createLSSerializer(); try (FileOutputStream outStream = new FileOutputStream(...)) { output.setByteStream(outStream); output.setEncoding("UTF-8"); ser.write(document, output); }
public static void writeToOutputStream(Node node, OutputStream outputStream, Encoding encoding) throws IOException { LSSerializer xmlSerializer = getSerializer(); LSOutput destination = DOM_IMPLEMENTATION.createLSOutput(); destination.setByteStream(outputStream); destination.setEncoding(encoding.getId()); try { xmlSerializer.write(node, destination); } catch (LSException exception) { throw new IOException("Error while writing document to output stream.", exception); } }
public void serialize(OutputStream os, Node node, Format format) throws XMLException { try { LSSerializer ser = createLSSerializer(format); LSOutput out = domImplementationLS.createLSOutput(); out.setByteStream(os); ser.write(node,out); } catch(Exception e) { throw new XMLException(e,"Error while converting XML document to string"); // $NLS-AbstractXercesDriver.ErrorwhileconvertingXMLdocumentto-1$ } }
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$ } }
protected LSOutput newLSOutput(DOMImplementationLS domImplementationLS) { final LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); return lsOutput; }