List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Main.java
/** * Save the XML document to a file./*from w ww . j a v a 2 s. com*/ * * @param doc * The XML document to save. * @param file * The file to save the document to. * @param encoding * The encoding to save the file as. * * @throws TransformerException * If there is an error while saving the XML. */ public static void save(Document doc, String file, String encoding) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file Result result = new StreamResult(new File(file)); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }
From source file:Main.java
/** * Save the XML document to an output stream. * /*from w w w.j a va 2 s . com*/ * @param doc * The XML document to save. * @param outStream * The stream to save the document to. * @param encoding * The encoding to save the file as. * * @throws TransformerException * If there is an error while saving the XML. */ public static void save(Node doc, OutputStream outStream, String encoding) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file Result result = new StreamResult(outStream); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }
From source file:com.github.woozoo73.ht.XmlUtils.java
public static String getContent(String path) { try {//from w w w . ja v a 2 s .c om URL url = XmlUtils.class.getClassLoader().getResource(path); File schemaLocation = new File(url.toURI()); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(schemaLocation); StringWriter writer = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(document), new StreamResult(writer)); String content = writer.getBuffer().toString(); return content; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String toString(Document doc) { try {/* www .j a v a 2 s. co m*/ StringWriter sw = new StringWriter(); Source source = new DOMSource(doc); Result result = new StreamResult(sw); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(4)); Transformer xformer = tf.newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); return sw.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Write DOM to a file// www .j a v a 2s . c o m * * @param doc * DOM document * @param filename * target file name * @param encoding * specified encoding * @param omitXmlDeclaration * flag to indicate if xml declaration statement is included * @throws Exception */ public static void writeXmlFile(Document doc, String filename, String encoding, String omitXmlDeclaration) throws Exception { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file FileOutputStream outputStream = new FileOutputStream(new File(filename)); Result result = new StreamResult(outputStream); // Write the DOM document to the file Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration); transformer.transform(source, result); outputStream.flush(); outputStream.close(); }
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. *///from w w w .j a v a 2s. 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 toStringE(Node element) { try {/*from www . j av a2s .c om*/ if (element == null) { return "null"; } Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { Result result = new StreamResult(printWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } return stringWriter.toString(); } catch (IllegalArgumentException | TransformerException ex) { throw new Error(ex); } }
From source file:Main.java
/** * Makes the specified XML string pretty. * //from w w w. j a v a2 s . co m * @param xmlString * The XML string to process. * @return Pretty-printed XML string. */ public static final String prettyPrint(final String xmlString) { try (InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes()); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); final Document document = documentBuilder.parse(inputStream); final TransformerFactory tfactory = TransformerFactory.newInstance(); final Transformer serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); final DOMSource xmlSource = new DOMSource(document); final StreamResult outputTarget = new StreamResult(baos); serializer.transform(xmlSource, outputTarget); return baos.toString("utf-8"); } catch (ParserConfigurationException | TransformerException | SAXException | IOException ex) { throw new RuntimeException("Can't pretty print xml!", ex); } }
From source file:Main.java
/** * Save the XML document to a file./*w w w .ja v a 2s . c om*/ * * @param doc * The XML document to save. * @param file * The file to save the document to. * @param encoding * The encoding to save the file as. * * @throws TransformerException * If there is an error while saving the XML. */ public static void save(Document doc, String file, String encoding) throws TransformerException { try { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file final Result result = new StreamResult(new File(file)); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
/** * Save the XML document to an output stream. * //from ww w . j ava 2 s .com * @param doc * The XML document to save. * @param outStream * The stream to save the document to. * @param encoding * The encoding to save the file as. * * @throws TransformerException * If there is an error while saving the XML. */ public static void save(Node doc, OutputStream outStream, String encoding) throws TransformerException { try { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file final Result result = new StreamResult(outStream); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }