List of usage examples for javax.xml.transform.stream StreamResult getWriter
public Writer getWriter()
From source file:Main.java
/** * Returns an XML representation of the provided node. * * @param node the node to be represented in XML. * * @return a string containing an XML representation of the * provided DOM node.//from w ww .j a v a 2 s . co m */ public static String nodeToXmlString(Node node) { try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(new StringWriter()); t.transform(source, result); return result.getWriter().toString(); } catch (TransformerException e) { throw new IllegalStateException(e); } catch (TransformerFactoryConfigurationError e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static void nodeToString(Node node, StringBuffer buf) throws TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource dSource = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(dSource, sr);/*from w w w .j a v a 2 s . c om*/ StringWriter anotherSW = (StringWriter) sr.getWriter(); buf.append(anotherSW.getBuffer()); }
From source file:Main.java
public static String xmlToString(Document doc) { try {//from w ww .j a v a2s .co m Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { ex.printStackTrace(); } return null; }
From source file:com.mlo55.utils.XMLUtils.java
/** * Prettifies XML data, e.g. add indentation. * @throws Exception/*from w ww .j a v a 2 s .c o m*/ */ public static String formatXml(String result) throws Exception { Source source = new StringSource(result); Transformer transformer = getTransformer(); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(source, streamResult); final String xmlOut = streamResult.getWriter().toString(); return xmlOut; }
From source file:Main.java
public static String DocToString(Document doc) throws TransformerFactoryConfigurationError, TransformerException { if (doc == null) return new String(""); String xmlString = ""; 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); xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String prettyPrintXML(String xml) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static String getXMLAsString(Document doc) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static String getXMLAsString(Element eElement) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(eElement); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
/** * Transforms an xml-file (xmlFilename) using an xsl-file (xslFilename) and * writes the output into file (outputFilename). * //from w w w . j a v a 2s .c o m * @param xmlFile * File: the xml-source-file to be transformed * @param xslFile * File: the xsl-file with the transformation rules * @param outputFilename * String: the name of the file the result will be written to */ public static void applyXSL(File xmlFile, File xslFile, String outputFilename) { try { // DocumentBuilderFactory docBFactory = DocumentBuilderFactory // .newInstance(); // DocumentBuilder docBuilder = docBFactory.newDocumentBuilder(); StreamSource xslStream = new StreamSource(xslFile); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(xslStream); StreamSource xmlStream = new StreamSource(xmlFile); StreamResult result = new StreamResult(new BufferedWriter(new FileWriter(outputFilename))); transformer.transform(xmlStream, result); result.getWriter().close(); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:Main.java
/** * Returns the String-Representation of the given DOM-Node as well-formed DOM-Document. * * @param node DOM-Node to print//from www. ja v a 2 s. co m * @param indent if true resulting XML is endented * @return <code>String</code> - Node as XML-String * @throws Exception on error */ public static String domNode2String(Node node, boolean indent) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); String xmlString = result.getWriter().toString(); return xmlString; }