List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:com.ibm.rpe.web.service.docgen.impl.GenerateBaseTemplate.java
private static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {// w w w .ja va 2 s . c o m Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString(); }
From source file:com.cisco.dvbu.ps.common.adapters.util.XmlUtils.java
public static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); try {/*from w ww.j a va2s. co m*/ Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { log.error("Xml to String Transformation error!" + te.getMessage()); throw te; } return sw.toString(); }
From source file:Main.java
/** * @return a transformer that indents entries by 4 characters (never null) *//* w ww .j ava 2 s. c om*/ public static final Transformer createIndentingTransformer() { Transformer xformer; try { xformer = transformerFactory.newTransformer(); } catch (Exception ex) { throw new IllegalStateException(ex); } xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); return xformer; }
From source file:Main.java
public static void serializeXML(Node e, Writer out) throws Exception { DOMSource domSource = new DOMSource(e); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); // turn off <?xml...?> stuff as for documents that were parsed with // non-UTF8 encoding, serializer inserts encoding="[non-utf-8]" there which // it should not, since we always serialize as UTF-8 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); }
From source file:org.opencastproject.remotetest.util.Utils.java
/** * Converts the node to a string representation. * /*from w w w . j ava 2 s .c om*/ * @param node * the node * @return the string representation * @throws Exception */ public static String nodeToString(Node node) throws Exception { DOMSource domSource = new DOMSource(node); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(domSource, result); InputStream in = new ByteArrayInputStream(out.toByteArray()); return IOUtils.toString(in, "UTF-8"); }
From source file:Main.java
public static String tryFormattingString(String s) { try {/* w ww . ja va 2 s. c o m*/ ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { } @Override public void fatalError(SAXParseException exception) throws SAXException { } @Override public void error(SAXParseException exception) throws SAXException { } }); Document doc = builder.parse(in); TransformerFactory tf = TransformerFactory.newInstance(); // tf.setAttribute("indent-number", new Integer(2)); Transformer trans = tf.newTransformer(); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult result = new StreamResult(out); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.transform(source, result); return out.toString(); } catch (Exception e) { // Console.println("Could not validate the soapmessage, although I'll show it anyway.."); } return s; }
From source file:Main.java
/** * @return a transformer that indents entries by 4 characters (never null) *//*from w w w .j a va 2s .c o m*/ public static final Transformer createIndentingTransformer() { Transformer transformer; try { transformerFactory.setAttribute("indent-number", 4); transformer = transformerFactory.newTransformer(); } catch (Exception e) { throw new IllegalStateException(e); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); return transformer; }
From source file:Main.java
/** * Writes an XML node to a writer./*from w w w . ja v a 2 s . co m*/ * @param node the XML node * @param writer the writer * @param outputProperties the output properties * @throws TransformerException if there's a problem writing to the writer */ public static void toWriter(Node node, Writer writer, Map<String, String> outputProperties) throws TransformerException { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); for (Map.Entry<String, String> property : outputProperties.entrySet()) { try { transformer.setOutputProperty(property.getKey(), property.getValue()); } catch (IllegalArgumentException e) { //ignore invalid output properties } } DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(writer); transformer.transform(source, result); } catch (TransformerConfigurationException e) { //no complex configurations } catch (TransformerFactoryConfigurationError e) { //no complex configurations } }
From source file:edu.harvard.i2b2.fhir.Utils.java
public static String getStringFromDocument(Document doc) { try {//from www .j a va2s.co m doc.normalize(); DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); // transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); int indent = 2; if (indent > 0) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent)); } // transformer.transform(domSource, result); return writer.toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
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 www.jav a 2 s . c o m*/ }