List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static String nodeToString(Node node) throws TransformerException { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
/** * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out). * * @param _docOrNode {@link Document} or {@link Node} object * @param _outStream {@link OutputStream} to print on * @throws IOException on error/*from w ww . j ava2s . co m*/ */ public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException { if (_docOrNode == null || _outStream == null) { throw new IOException("Cannot print (on) 'null' object"); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(_docOrNode), new StreamResult(new OutputStreamWriter(_outStream, "UTF-8"))); } catch (UnsupportedEncodingException | TransformerException _ex) { throw new IOException("Could not print Document or Node.", _ex); } }
From source file:Main.java
public static String xmlToString(Document doc) { try {/*from ww w .java 2s . c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { System.err.println("Error: Cannot load xml transformer"); System.err.println("Cause: " + ex.toString()); System.exit(1); return null; } }
From source file:Main.java
public static void writeToFile(Document doc, String file) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); StreamResult result = new StreamResult(pw); transformer.transform(source, result); }
From source file:Main.java
/** * Writes a DOM document to a file.// w w w . j a v a 2 s .c o m * * @param document * DOM document. * @param fileName * target file path. */ public static void writeDOMToFile(Document document, String fileName) { FileOutputStream fos; Source source; Result result; Transformer xformer; try { fos = new FileOutputStream(fileName); source = new DOMSource(document); result = new StreamResult(fos); xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String GetStringFromDoc(Document d) { /*/*from www .j av a 2s . c o m*/ DOMImplementationLS domImplementation = (DOMImplementationLS) d.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(d); */ StringWriter output = new StringWriter(); try { //Transformer transformer = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null).newTransformer(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(d.getDocumentElement()), new StreamResult(output)); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output.toString(); }
From source file:Main.java
public static Templates TrAXPath(String xpath) throws TransformerConfigurationException { StringBuffer sb = new StringBuffer(); // Create stylesheet that copies matching nodes sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); sb.append("<xsl:stylesheet version=\"1.0\" "); sb.append(" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"); sb.append("<xsl:output method=\"xml\" indent=\"yes\"/>"); sb.append("<xsl:template match=\"" + xpath + "\">"); sb.append("<xsl:copy-of select=\".\"/>"); sb.append("</xsl:template>"); sb.append("<xsl:template match=\"*|@*|text()\">"); sb.append("<xsl:apply-templates />"); sb.append("</xsl:template>"); sb.append("</xsl:stylesheet>"); // Construct the stylesheet Templates object. TransformerFactory tf = TransformerFactory.newInstance(); String stylesheet = sb.toString(); java.io.Reader r = new java.io.StringReader(stylesheet); StreamSource ssrc = new StreamSource(r); // Create templates object return tf.newTemplates(ssrc); }
From source file:Main.java
private static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
private static String getNodeValue(NodeList nodeList) throws TransformerFactoryConfigurationError, TransformerException { final Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setURIResolver(null);//from www .j a va2 s . c om final StringBuilder buf = new StringBuilder(); for (int i = 0; i < nodeList.getLength(); i++) { final StringWriter sw = new StringWriter(); serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw)); String xml = sw.toString(); final Matcher matcher = HEADER_PATTERN.matcher(xml); if (matcher.matches()) { xml = matcher.group(1); } buf.append(xml); buf.append("\n"); } return buf.toString(); }
From source file:Main.java
public static String serializeDocument(Document document) throws Exception { // Serialize XML document to String. StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); DOMSource domSource = new DOMSource(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return writer.toString(); }