List of usage examples for javax.xml.transform.dom DOMSource DOMSource
public DOMSource(Node n)
From source file:Main.java
/** * Save an XML document into a file/*from www. j ava 2s.com*/ * * @param document * the XML document to save * @param file * the file * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void docToFile(final Document document, final File file) throws TransformerFactoryConfigurationError, TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.transform(new DOMSource(document), new StreamResult(file)); }
From source file:Main.java
private static String xmlToString(Node node) { try {// ww w . j av a 2s . co m Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void writeXmlFile(Document doc, File file) throws TransformerConfigurationException, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(file); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); }
From source file:Main.java
public static String nodeToString(Node n, boolean isOmitXmlDeclaration, boolean isIndent) throws TransformerException { StringWriter sw = new StringWriter(); Transformer transformer = generateTransformer(isOmitXmlDeclaration, isIndent); transformer.transform(new DOMSource(n), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
/** * DOM to string.// w ww.ja v a 2 s. c o m * * @param doc * the doc * @return the string */ /* * from: * http://www.journaldev.com/71/utility-java-class-to-format-xml-document * -to-xml-string-and-xml-to-document */ public static String DOMToString(Document doc) { String xmlString = ""; if (doc != null) { try { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); xmlString = sw.toString(); } catch (Exception e) { e.printStackTrace(); } } return xmlString; }
From source file:Main.java
public static String printXMLNode(Node node) { try {/*from w w w .j ava 2s. c om*/ TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 2); Transformer transformer = tf.newTransformer(); String omitDeclaration = node instanceof Document || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes"; transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); String output = writer.getBuffer().toString(); output = removeEmptyLines(output); // .replaceAll("\n|\r", ""); return output; } catch (TransformerException te) { throw new RuntimeException(te); } }
From source file:Main.java
public static void writeDocument(Document document, OutputStream out) throws TransformerException { TransformerFactory transformFactory = TransformerFactory.newInstance(); Transformer idTransform = transformFactory.newTransformer(); Source input = new DOMSource(document); Result output = new StreamResult(out); idTransform.transform(input, output); }
From source file:Main.java
public static String nodeToString(Node node) throws TransformerException { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void print(Document doc, Writer out) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(out); transformer.transform(source, result); }
From source file:Main.java
/** Write a DOM document to a string */ public static String xmlDocumentToString(Document document) { try {// ww w. j a va 2 s. co m Source source = new DOMSource(document); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }