List of usage examples for javax.xml.transform.stream StreamResult getWriter
public Writer getWriter()
From source file:Main.java
public static String transform(String xmlFilePath, String xsltFilePath) throws Exception { StreamResult result = new StreamResult(new StringWriter()); StreamSource s = new StreamSource(new File(xsltFilePath)); StreamSource xml = new StreamSource(new File(xmlFilePath)); Transformer transformer = TransformerFactory.newInstance().newTransformer(s); transformer.transform(xml, result);// w w w. j ava2s . c o m String response = result.getWriter().toString(); return response; }
From source file:nl.igorski.lib.utils.data.XMLTool.java
/** * perform a full dump of a XML Document * for debugging purposes/* w w w . j a v a2 s . c o m*/ * * @param doc {Node} * @return {String} */ public static String nodeToString(Node doc) { Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } catch (TransformerConfigurationException e) { } if (transformer != null) { //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); try { transformer.transform(source, result); } catch (TransformerException e) { } return result.getWriter().toString(); } return null; }
From source file:Main.java
public static String transformString(String xmlString, String xsltFilePath) throws Exception { StreamResult result = new StreamResult(new StringWriter()); StreamSource s = new StreamSource(new File(xsltFilePath)); StreamSource xml = new StreamSource(new StringReader(xmlString)); Transformer transformer = TransformerFactory.newInstance().newTransformer(s); transformer.transform(xml, result);//from w w w .j a v a 2 s .c o m String response = result.getWriter().toString(); return response; }
From source file:ca.uviccscu.lp.utils.Utils.java
public static String documentToString(Document d) throws TransformerConfigurationException, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(d); transformer.transform(source, result); String str = result.getWriter().toString(); return str;//from w w w . j a v a2 s .c o m }
From source file:Main.java
/** * Adds a new node to a file.//ww w . java 2 s . c o m * * @param nodeType {@link String} The type of the element to add. * @param idField {@link String} The name of the field used to identify this * node. * @param nodeID {@link String} The identifier for this node, so its data * can be later retrieved and modified. * @param destFile {@link File} The file where the node must be added. * @param attributes {@link ArrayList} of array of String. The arrays must * be bidimensional (first index must contain attribute name, second one * attribute value). Otherwise, an error will be thrown. However, if * <value>null</value>, it is ignored. */ public static void addNode(String nodeType, String idField, String nodeID, File destFile, ArrayList<String[]> attributes) { if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { if (it.next().length != 2) { throw new IllegalArgumentException("Invalid attribute combination"); } } } /* * XML DATA CREATION - BEGINNING */ DocumentBuilder docBuilder; Document doc; try { docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = docBuilder.parse(destFile); } catch (SAXException | IOException | ParserConfigurationException ex) { return; } Node index = doc.getFirstChild(), newElement = doc.createElement(nodeType); NamedNodeMap elementAttributes = newElement.getAttributes(); Attr attrID = doc.createAttribute(idField); attrID.setValue(nodeID); elementAttributes.setNamedItem(attrID); if (attributes != null) { for (Iterator<String[]> it = attributes.iterator(); it.hasNext();) { String[] x = it.next(); Attr currAttr = doc.createAttribute(x[0]); currAttr.setValue(x[1]); elementAttributes.setNamedItem(currAttr); } } index.appendChild(newElement); /* * XML DATA CREATION - END */ /* * XML DATA DUMP - BEGINNING */ Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException ex) { return; } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); try { transformer.transform(source, result); } catch (TransformerException ex) { return; } String xmlString = result.getWriter().toString(); try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFile))) { bufferedWriter.write(xmlString); } catch (IOException ex) { } /* * XML DATA DUMP - END */ }
From source file:Main.java
public static String format(String input, int indent) { try {/*from ww w. j av a2 s . c o m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * /*from w w w .j a v a 2s . co m*/ * @param xml * @param indent * @return pretty formatted xml */ public static String prettyFormat(String xml, int indent) { try { Source xmlInput = new StreamSource(new StringReader(xml)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
/** * Formats a xml string//w ww . ja v a2 s .com * @param input * @param indent * @return */ public static String prettyFormatXml(String input, int indent) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); } return input; }
From source file:Main.java
public static String prettifyXmlString(String uglyXml) { Transformer transformer = null; try {/*from ww w. j av a2 s . c om*/ transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = null; try { source = new DOMSource(string2Document(uglyXml)); } catch (Exception e) { e.printStackTrace(); } try { transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String toString(Node doc) throws Exception { DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new StringWriter()); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, result); return result.getWriter().toString(); }