List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static void createFile(Document document, String filePath) throws TransformerException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(document); Result dest = new StreamResult(new File(filePath)); aTransformer.transform(src, dest);/*from w w w . java 2s. c o m*/ }
From source file:Main.java
private static TransformerFactory getTransformerFactory() { //return TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null); if (transFactory == null) { //transFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null); transFactory = TransformerFactory.newInstance(); /*/*from w w w . j a va2s . c o m*/ try { transFactory.setFeature("http://xml.org/sax/features/validation", false); transFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); transFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block logger.error(e.getMessage()); e.printStackTrace(); } */ } return transFactory; }
From source file:Main.java
public static String printDocument(Node doc) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 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", "2"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String printDocument(Node doc) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 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", "2"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void setDocument(Document document, String fileName) { try {// w w w . j ava2s . co m TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(new File(fileName)); transformer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String prettyPrintXML(String xml) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
/** * @param xml/*from ww w .j a va2 s .c om*/ * @return pretty xml */ public static String prettyXml(String xml) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { Source source = new StreamSource(new StringReader(xml)); StringWriter writer = new StringWriter(); serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (Exception e) { return xml; } }
From source file:Main.java
public static String elementToString(final Node node) throws Exception { final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 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"); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final Document document = factory.newDocumentBuilder().newDocument(); final Node importedNode = document.importNode(node, true); document.appendChild(importedNode);/*from ww w. j a v a2s. com*/ final ByteArrayOutputStream bos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(document), new StreamResult(bos)); return bos.toString("UTF-8"); }
From source file:Main.java
public static String toString(Node doc) throws Exception { DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new StringWriter()); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, result); return result.getWriter().toString(); }
From source file:Main.java
public static void saveXMLDocumentAsFile(Document doc, File file) { try {//from www . j a v a 2 s . c o m // Indent & Doctype declaration Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // Prepares to write StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); // Output to file result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { System.out.println("Error on saving XML file."); e.printStackTrace(); } }