List of usage examples for org.w3c.dom.ls LSOutput setCharacterStream
public void setCharacterStream(java.io.Writer characterStream);
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from ww w . j ava 2s . c o m DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElementNS(null, "person"); // Create Root Element Element item = doc.createElementNS(null, "name"); // Create element item.appendChild(doc.createTextNode("Jeff")); root.appendChild(item); // Attach element to Root element item = doc.createElementNS(null, "age"); // Create another Element item.appendChild(doc.createTextNode("28")); root.appendChild(item); // Attach Element to previous element down tree item = doc.createElementNS(null, "height"); item.appendChild(doc.createTextNode("1.80")); root.appendChild(item); // Attach another Element - grandaugther doc.appendChild(root); // Add Root to Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer // for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(doc, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // DOM as a String }
From source file:DOMGenerate.java
public static void main(String[] argv) { try {//from w w w.jav a 2 s . co m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElementNS(null, "person"); // Create Root Element Element item = doc.createElementNS(null, "name"); // Create element item.appendChild(doc.createTextNode("Jeff")); root.appendChild(item); // Attach element to Root element item = doc.createElementNS(null, "age"); // Create another Element item.appendChild(doc.createTextNode("28")); root.appendChild(item); // Attach Element to previous element down tree item = doc.createElementNS(null, "height"); item.appendChild(doc.createTextNode("1.80")); root.appendChild(item); // Attach another Element - grandaugther doc.appendChild(root); // Add Root to Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer // for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(doc, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // Spit out the // DOM as a String } catch (Exception ex) { ex.printStackTrace(); } }
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 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 www . j a va 2 s . co m*/ 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
private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException { try {//from w w w . j av a 2s. co m DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer serializer = impl.createLSSerializer(); DOMConfiguration config = serializer.getDomConfig(); config.setParameter("xml-declaration", addXmlDeclaration); // config.setParameter("format-pretty-print", true); // config.setParameter("normalize-characters", true); LSOutput out = impl.createLSOutput(); out.setCharacterStream(writer); serializer.write(doc, out); } catch (Throwable e) { throw new IOException(e.getMessage()); } }
From source file:Main.java
/** * Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer. The written content is encoded using * the encoding specified in the writer configuration. * /* w w w . j ava2 s .c o m*/ * @param node the node to write out * @param output the writer to write the XML to * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer * instance, obtained via {@link LSSerializer#getDomConfig()}. May be null. */ public static void writeNode(Node node, Writer output, Map<String, Object> serializerParams) { DOMImplementationLS domImplLS = getLSDOMImpl(node); LSSerializer serializer = getLSSerializer(domImplLS, serializerParams); LSOutput serializerOut = domImplLS.createLSOutput(); serializerOut.setCharacterStream(output); serializer.write(node, serializerOut); }
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);/*from w ww.ja v a 2 s . c o m*/ return domOutput.getCharacterStream().toString(); }
From source file:Main.java
/** * Serialize XML Node to string/*from w w w. j a v a 2s . com*/ * <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(); }
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 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 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 av 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."); } }