List of usage examples for javax.xml.transform.dom DOMSource DOMSource
public DOMSource(Node n)
From source file:Main.java
public static String xmlDocumentToString(final Document doc) { try {/*ww w . j a va 2s. c o m*/ final DOMSource domSource = new DOMSource(doc); final StringWriter writer = new StringWriter(); final StreamResult result = new StreamResult(writer); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch (final TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
public static String domToText(Document inDocument) throws TransformerException { StringWriter output = new StringWriter(); DOMSource source = new DOMSource(inDocument); StreamResult result = new StreamResult(output); transformer.transform(source, result); //this.getWatcher().setProgressText("saved " + getEditedFile().getAbsolutePath()); return output.toString(); }
From source file:Main.java
public static String getDocumentAsXml(Document doc) { StringWriter sw = new java.io.StringWriter(); try {// ww w. j a va 2 s .co m DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static String formatDocument2(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return new String(out.toByteArray()); }
From source file:Main.java
public static synchronized void write2Xml(Document document) throws Exception { TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(new FileOutputStream(filepath))); }
From source file:Main.java
public static void DocumentToStream(final Document doc, OutputStream stream) { Result l_s = new StreamResult(stream); doc.normalize();// w ww. jav a 2 s . co m try { TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s); stream.close(); } catch (Exception e) { System.err.println(e); return; } }
From source file:Main.java
/** Write an XML DOM tree to a file * * @param doc the document to write//from w w w.ja v a 2s . c o m * @param file the file to write to * @throws IOException if a file I/O error occurs */ public static void write(Document doc, File file) throws IOException { try { DOMSource domSource = new DOMSource(doc); FileOutputStream stream = new FileOutputStream(file); // OutputStreamWriter works around indent bug 6296446 in JDK // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446 StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream)); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(2)); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); } catch (TransformerException e) { // This exception is never thrown, treat as fatal if it is throw new RuntimeException(e); } }
From source file:Main.java
public static void print(Node dom) { try {/*from w ww .j a va 2 s . c om*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer t = factory.newTransformer(); StringWriter sw = new StringWriter(); t.transform(new DOMSource(dom), new StreamResult(sw)); System.out.println(sw.toString()); } catch (Exception ex) { System.out.println("Could not print XML document"); } }
From source file:Main.java
public static String xmlParaString(Node doc) { DOMSource xmlSource = new DOMSource(doc); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer;/*w w w . j a v a 2 s .co m*/ try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } try { transformer.transform(xmlSource, result); } catch (TransformerException e) { throw new RuntimeException(e); } return sw.toString(); }
From source file:Main.java
public static boolean debug(Node paramNode) { try {/*from w w w . java 2 s .co m*/ TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); DOMSource localDOMSource = new DOMSource(paramNode); StreamResult localStreamResult = new StreamResult(System.out); localTransformer.transform(localDOMSource, localStreamResult); return true; } catch (Exception localException) { localException.printStackTrace(System.out); } return false; }