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
public static void save(Document document, IFile file, IProgressMonitor monitor) throws TransformerException, CoreException, UnsupportedEncodingException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource ds = new DOMSource(document); trans.transform(ds, sr);/*ww w . j a va 2 s. c o m*/ String xmlString = sw.toString(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); if (file.exists()) { file.setContents(is, 0, monitor); } else { file.create(is, true, monitor); } }
From source file:Main.java
public static String xmltoString(final Document document) throws TransformerException { StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(document.getDocumentElement()), streamResult); return stringWriter.toString(); }
From source file:Main.java
public static Transformer buildTransformer() throws TransformerConfigurationException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); return transformer; }
From source file:Main.java
/** * Updates existing file with Document object * /*from w ww .j ava2s. co m*/ * @param doc * xml document * @param file * xml file to update * @throws TransformerFactoryConfigurationError */ public static void updateXMLFile(Document doc, File file) throws TransformerFactoryConfigurationError { try { DOMSource source = new DOMSource(doc); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException("Exception while saving message to xml file", e); } }
From source file:Main.java
public static void formatDocument(Document document, Writer out, String encoding) throws TransformerException { document.normalizeDocument();//from ww w . ja v a 2 s.c o m TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer idTransform = transFactory.newTransformer(); idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); idTransform.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { idTransform.setOutputProperty(OutputKeys.ENCODING, encoding); } idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source source = new DOMSource(document); Result result = new StreamResult(out); idTransform.transform(source, result); }
From source file:Main.java
/** * Get DOM as a string/* w ww .j ava2s. co m*/ * @param doc * @return */ public static String getStringFromDoc(org.w3c.dom.Document doc) { if (doc == null) { System.out.println("XML document is null"); } try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://commons.omniupdate.com/dtd/standard.dtd"); transformer.transform(domSource, result); writer.flush(); return writer.toString(); } catch (TransformerException ex) { System.out.println("Transformer Exception"); // ex.printStackTrace(); return "Error in transformation"; } }
From source file:Main.java
public static String nodeToString(Node node) throws Exception { if (node == null) { return null; }/*from w ww . j a va2 s. c o m*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
static void serialize(Document document, Writer writer) { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", new Integer(4)); try {//from w ww. ja va2s . co m Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(writer); transformer.transform(new DOMSource(document), result); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void printDOM(Node node, OutputStream out, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer();/*from w ww . j a v a 2s . c o m*/ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(out)); }
From source file:Main.java
public static void saveDocument(final Document document, String filename) throws TransformerException { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); t.transform(source, result);//from ww w .j av a 2s . c om }