List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
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 ava 2 s .c o m*/ }
From source file:Main.java
/** * Converts a dom to a String/*from w ww .ja v a 2 s. co m*/ * * @param dom dom to convert * @param outputProperties the properties for the String representation of * the XML * @return the dom as a String */ public static String writeDomToString(Document dom, Properties outputProperties) { try { StringWriter ret = new StringWriter(); TransformerFactory transFact = TransformerFactory.newInstance(); // transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(); if (outputProperties != null) { transformer.setOutputProperties(outputProperties); } DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(ret); transformer.transform(source, result); return ret.toString(); } catch (Exception e) { throw new RuntimeException("Could not write dom to string!", e); } }
From source file:Main.java
/** * Perform an xsl transformation//from w w w . j a v a 2 s. c om */ public static void transform(Source xsl, Source xml, Result result) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Templates template = factory.newTemplates(xsl); Transformer transformer = template.newTransformer(); transformer.transform(xml, result); }
From source file:Main.java
public static void nodeToWriter(Node node, Writer writer) throws Exception { 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); StreamResult result = new StreamResult(writer); transformer.transform(source, result); }
From source file:Main.java
public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static final void prettyPrint(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); System.out.println(out.toString()); }
From source file:Main.java
private static String getXmlString(Document document, String tagName) throws ParserConfigurationException, SAXException, IOException, TransformerException { NodeList nodeList = document.getElementsByTagName(tagName); if (nodeList.getLength() < 1) { throw new IllegalArgumentException("no " + tagName + " found"); }/*from w w w . ja va2s.c o m*/ Node sub = nodeList.item(0); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document subdoc = db.newDocument(); subdoc.appendChild(sub.cloneNode(true)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(subdoc); StringWriter sw = new StringWriter(); StreamResult xmlResult = new StreamResult(sw); transformer.transform(domSource, xmlResult); sw.flush(); return sw.toString(); }
From source file:Main.java
public static String getXml(Document doc) { try {/*from ww w. jav a 2 s .c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = result.getWriter().toString(); return xmlString; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
License:asdf
public static String documentToString(Document document) { try {//from ww w . j a va2 s . c om TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString(); } catch (TransformerException tEx) { tEx.printStackTrace(); } return null; }
From source file:Main.java
public static void writeTransformedXml(Document doc, OutputStream output, InputStream style) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); StreamSource stylesource = new StreamSource(style); Transformer transformer = transformerFactory.newTransformer(stylesource); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(output); transformer.transform(source, result); }