List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Main.java
/** Returns the entier value of a node (child nodes and text) as a String * @param node/*from ww w .j a v a 2 s .co m*/ * @return */ public static String nodeChildrenToString(Node node) { StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) t.transform(new DOMSource(nl.item(i)), new StreamResult(sw)); // dimes.AgentGuiComm.GUICommunicator.sendLog(Level.WARNING, "", sw.toString()); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } String test = sw.toString(); return sw.toString().trim(); }
From source file:Main.java
/** * Creates a string representation of a {@link Node} instance. This method * does not introduce any character to the string representation of the * {@link Node} (eg. \n or \r characters) * * @param node A {@link Node} instance/*w w w.j av a 2 s . c o m*/ * @return A string representation of the node instance * @throws RequestSecurityTokenException */ public static String xmlToString(Node node) throws Exception { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } catch (TransformerException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } }
From source file:Main.java
/** * Formats a xml string/*from w w w.ja v a 2s.c o m*/ * @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
/** * convert an XML node to an XML statement * @param node current XML node//w ww . j av a 2s . c o m * @return XML string */ public static String nodeToString(Node node) { //MagicBooleans.trace("nodeToString(node: " + node + ")"); StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } String result = sw.toString(); //MagicBooleans.trace("nodeToString() returning: " + result); return result; }
From source file:Main.java
public static String pretty(String xml) { try {/*ww w.j a va 2s . com*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); Source source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n"); } catch (TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * DOM to string./*from w w w . j av 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 formatXmlAsString(Document document) { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); try {/*from ww w .j a v a 2 s . co m*/ factory.setAttribute("indent-number", new Integer(2)); } catch (IllegalArgumentException ex) { } try { Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException ex) { throw new RuntimeException("Error formatting xml as pretty-printed string", ex); } return writer.toString(); }
From source file:Main.java
private static void WriteXMLFile(Document doc, OutputStreamWriter w, String encoding) { try {/* w w w . j ava 2 s . c o m*/ Source source = new DOMSource(doc); Result ret = new StreamResult(w); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); xformer.transform(source, ret); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static byte[] toByteArray(Node doc) { try {/*from w w w . j a va 2 s .c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); ByteArrayOutputStream os = new ByteArrayOutputStream(); StreamResult result = new StreamResult(os); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return os.toByteArray(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void buildXml(Node node, Writer writer) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(node), new StreamResult(writer)); }