@Override public void serialize(Node value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_domImpl == null) throw new IllegalStateException("Could not find DOM LS"); LSSerializer writer = _domImpl.createLSSerializer(); jgen.writeString(writer.writeToString(value)); }
@Override public void serialize(Node value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_domImpl == null) throw new IllegalStateException("Could not find DOM LS"); LSSerializer writer = _domImpl.createLSSerializer(); jgen.writeString(writer.writeToString(value)); }
public String innerXml(Node node) { DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0"); LSSerializer lsSerializer = lsImpl.createLSSerializer(); NodeList childNodes = node.getChildNodes(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < childNodes.getLength(); i++) { sb.append(lsSerializer.writeToString(childNodes.item(i))); } return sb.toString(); }
Document document = node.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document .getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); String str = serializer.writeToString(node);
@Override public void serialize(Node value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_domImpl == null) throw new IllegalStateException("Could not find DOM LS"); LSSerializer writer = _domImpl.createLSSerializer(); jgen.writeString(writer.writeToString(value)); }
public String getStringFromDoc(org.w3c.dom.Document doc) { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(doc); }
private String elementToString(Element root) { DOMImplementationLS domImplLS = (DOMImplementationLS) root.getOwnerDocument().getImplementation(); return domImplLS.createLSSerializer().writeToString(root); } }
@Override public void serialize(Node value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_domImpl == null) throw new IllegalStateException("Could not find DOM LS"); LSSerializer writer = _domImpl.createLSSerializer(); jgen.writeString(writer.writeToString(value)); }
LSSerializer serializer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8");
final LSSerializer writer = impl.createLSSerializer();
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); } }
LSSerializer lss = ls.createLSSerializer(); LSOutput lso = ls.createLSOutput(); lso.setByteStream(System.out);
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); } }
output.setEncoding("UTF-8"); output.setByteStream(baos); LSSerializer ser = ls.createLSSerializer(); String fpp = "format-pretty-print"; // NOI18N if (ser.getDomConfig().canSetParameter(fpp, true)) {
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."); } }
@Override public void serialize(Node value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_domImpl == null) throw new IllegalStateException("Could not find DOM LS"); LSSerializer writer = _domImpl.createLSSerializer(); jgen.writeString(writer.writeToString(value)); }
serializer = domImplLS.createLSSerializer(); DOMConfiguration config = serializer.getDomConfig(); if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
serializer = domImplLS.createLSSerializer(); DOMConfiguration config = serializer.getDomConfig(); if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) {
private DOMSerializer() { DOMImplementationLS domImpl = Support.getImplementation(); serializer = domImpl.createLSSerializer(); output = domImpl.createLSOutput(); }