List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
public static boolean saveDocument(String fileName, Document doc) { System.out.println("Saving XML file... " + fileName); // open output stream where XML Document will be saved File xmlOutputFile = new File(fileName); FileOutputStream fos;//from w w w . j a v a2 s . c o m Transformer transformer; try { fos = new FileOutputStream(xmlOutputFile); } catch (FileNotFoundException e) { System.out.println("Error occured: " + e.getMessage()); return false; } // Use a Transformer for output TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { System.out.println("Transformer configuration error: " + e.getMessage()); return false; } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(fos); // transform source into result will do save try { transformer.transform(source, result); } catch (TransformerException e) { System.out.println("Error transform: " + e.getMessage()); } return true; }
From source file:Main.java
/** * Transforms an XML to a String// ww w . j a va2s .co m * @param node XML node * @return String represenation of XML */ public static String printXML(Node node) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { serializer = tfactory.newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); StringWriter output = new StringWriter(); serializer.transform(new DOMSource(node), new StreamResult(output)); return output.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** Transform a XML Document to String */ public static String toString(Document doc) { try {/*from ww w . java2 s . c o m*/ DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } }
From source file:Main.java
/** * Serialize XML from a {@link Document} to an OutputStream. * @param doc/* w w w . j a va 2 s . co m*/ * @param out */ public static void serialiseXml(Document doc, Writer out) { DOMSource domSource = new DOMSource(doc.getDocumentElement()); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = null; try { serializer = tf.newTransformer(); } catch (TransformerConfigurationException e) { } serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); try { serializer.transform(domSource, streamResult); } catch (TransformerException e) { } }
From source file:Main.java
public static void writeXMLDocToFile(Document outputDoc, String outFileName) { try {/*from w ww . j ava 2 s . c o m*/ //FileWriter writer = new FileWriter( outFileName ); final String encoding = "UTF-8"; OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFileName), encoding); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(outputDoc); StreamResult result = new StreamResult(writer); transformer.transform(source, result); writer.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not create output XML file: " + outFileName); } catch (TransformerConfigurationException tce) { // Error generated by the parser System.out.println("* Transformer Factory error"); System.out.println(" " + tce.getMessage()); // Use the contained exception, if any Throwable x = tce; if (tce.getException() != null) x = tce.getException(); x.printStackTrace(); } catch (TransformerException te) { // Error generated by the parser System.out.println("* Transformation error"); System.out.println(" " + te.getMessage()); // Use the contained exception, if any Throwable x = te; if (te.getException() != null) x = te.getException(); x.printStackTrace(); } }
From source file:Main.java
/** * Returns a String representation of the DOM hierarchy rooted at the argument Node. * @param node The root Node of the DOM hierarchy to translate. * @return A String representation of the DOM hierarchy rooted at the * argument Node, or null if the operation fails. *//*w ww . j a v a2 s. c o m*/ public static String toXMLString(Node node, boolean header) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); if (!header) transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } // try - catch return null; }
From source file:Main.java
public static String writeDocumentToString(Document document) { try {/* w ww . j a va 2s . com*/ TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); try { StreamResult streamResult = new StreamResult(writer); DOMSource source = new DOMSource(document); trans.transform(source, streamResult); } catch (TransformerException e) { e.printStackTrace(); return null; } finally { writer.close(); } return writer.toString(); } catch (TransformerConfigurationException | IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static void writeDocToFile(Document doc, String filename) throws Exception { Source source = new DOMSource(doc); File file = new File(filename); Result result = new StreamResult(file); TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", 2); Transformer xformer = tFactory.newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); xformer.transform(source, result);//ww w . j a v a2s . c o m /*OutputFormat format = new OutputFormat(doc); format.setIndenting(true); format.setIndent(2); Writer output = new BufferedWriter( new FileWriter(filename) ); XMLSerializer serializer = new XMLSerializer(output, format); serializer.serialize(doc);*/ }
From source file:Main.java
public static void output(Document doc, OutputStream outputStream) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", 4); Transformer transformer = tFactory.newTransformer(); Properties outputProperties = new Properties(); outputProperties.put(OutputKeys.INDENT, "yes"); outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperties(outputProperties); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new OutputStreamWriter(outputStream, "UTF-8")); transformer.transform(source, result); }
From source file:Main.java
/** * Transform this {@link Node} into a {@link String} representation. * * @param xml/*w w w . ja v a 2 s . c o m*/ * the xml Node * @return a String representation of the given xml Node */ public static String toString(Node xml) { short type = xml.getNodeType(); if (type == Node.TEXT_NODE) return xml.getNodeValue(); StringWriter buffer = new StringWriter(); try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(xml), new StreamResult(buffer)); } catch (IllegalArgumentException | TransformerException e) { //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'."); return ""; } return buffer.toString(); }