List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;//from w w w .ja va2s . c o m try { transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static boolean debug(Node paramNode) { try {// ww w. j a va 2 s. com TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); DOMSource localDOMSource = new DOMSource(paramNode); StreamResult localStreamResult = new StreamResult(System.out); localTransformer.transform(localDOMSource, localStreamResult); return true; } catch (Exception localException) { localException.printStackTrace(System.out); } return false; }
From source file:Main.java
public static synchronized void writeXmlFile(String xmlFile, Document document) throws Exception { // transform the document to the file TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer idTransformer = xformFactory.newTransformer(); idTransformer.setOutputProperty("indent", "yes"); Source input = new DOMSource(document); Result output = new StreamResult(new File(xmlFile)); idTransformer.transform(input, output); /*//from w ww . jav a 2s . c om OutputFormat format = new OutputFormat(); format.setLineSeparator(LineSeparator.Unix); format.setIndenting(true); format.setLineWidth(0); format.setPreserveSpace(true); XMLSerializer ser = new XMLSerializer(new FileWriter(xmlFile), format); ser.asDOMSerializer(); ser.serialize(document); */ return; }
From source file:Main.java
/** * Get cached TransformerFactory/* ww w . java 2 s .c om*/ * @return TransformerFactory */ public static TransformerFactory getTransformerFactory() { if (transformerFactory == null) { transformerFactory = TransformerFactory.newInstance(); } return transformerFactory; }
From source file:Main.java
public static String writeXMLtoString(org.w3c.dom.Document doc) throws Exception { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result);/*from w w w . j a v a 2s . c o m*/ String xmlString = sw.toString(); return xmlString; }
From source file:Main.java
public static String xmlToString(Document doc) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc.getDocumentElement()), new StreamResult(writer)); String result = writer.toString(); System.out.println(result);//from ww w . j a va2s. c o m return result; }
From source file:Main.java
public static void printNode(Node node) { try {/* ww w. j a v a 2 s .c om*/ // Set up the output transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // Print the DOM node StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(node); trans.transform(source, result); String xmlString = sw.toString(); System.out.println(xmlString); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static TransformerFactory newTransformerFactory() { factoryLock.lock();/*w w w. j ava 2 s.c o m*/ try { return TransformerFactory.newInstance(); } finally { factoryLock.unlock(); } }
From source file:Main.java
public static void pretty(Document document, OutputStream outputStream, int indent) throws IOException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = null; try {/* ww w. j ava 2 s. com*/ transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); if (indent > 0) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent)); } Result result = new StreamResult(outputStream); Source source = new DOMSource(document); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException(e); } }
From source file:Main.java
public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;//from ww w . ja v a 2 s .c o m try { serializer = tfactory.newTransformer(); if (prettyPrint) { //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } if (!includeXmlDeclaration) { serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } DOMSource xmlSource = new DOMSource(node); StreamResult outputTarget = new StreamResult(out); serializer.transform(xmlSource, outputTarget); } catch (TransformerException e) { throw new RuntimeException(e); } }