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
public static void serialize(Source source, Writer writer, Map<String, String> properties) throws Exception { StreamResult sr = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null; t = tf.newTransformer();// w w w . ja v a 2 s. c om for (Map.Entry<String, String> me : properties.entrySet()) { t.setOutputProperty(me.getKey(), me.getValue()); } t.transform(source, sr); }
From source file:Main.java
/** * Serialize an XML Element into a String. * @param e Element to be serialized./*www. jav a 2 s . c om*/ * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. */ public static final String serialize(Element e, boolean omitXMLDeclaration) { if (e != null) { try { DOMSource domSource = new DOMSource(e); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no")); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.transform(domSource, new StreamResult(baos)); baos.close(); return new String(baos.toByteArray(), UTF8_ENCODING); } catch (Throwable t) { } } return null; }
From source file:Main.java
/** * Serialize an XML Element into a String. * @param df DocumentFragment to be serialized. * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. *//*w w w . ja v a 2 s .c o m*/ public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) { if (df != null) { try { DOMSource domSource = new DOMSource(df); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no")); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.transform(domSource, new StreamResult(baos)); baos.close(); return new String(baos.toByteArray(), UTF8_ENCODING); } catch (Throwable t) { } } return null; }
From source file:Main.java
public static void printDOM(OutputStream ostr, Document doc, String encoding) { try {//from w w w . ja v a 2s . co m Transformer tr = TransformerFactory.newInstance().newTransformer(); if (encoding != null) tr.setOutputProperty(OutputKeys.ENCODING, encoding); tr.setOutputProperty(OutputKeys.INDENT, "yes"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); tr.transform(new DOMSource(doc), new StreamResult(ostr)); } catch (TransformerConfigurationException ex) { System.err.println(ex.getMessage()); } catch (TransformerException ex) { System.err.println(ex.getMessage()); } }
From source file:Main.java
/** * Uses a TransformerFactory with an identity transformation to convert a * Document into a String representation of the XML. * * @param document Document.//from w w w. ja v a2 s . c o m * @return An XML String. * @throws IOException if an error occurs during transformation. */ public static String documentToString(Document document) throws IOException { String xml = null; try { DOMSource dom = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult output = new StreamResult(writer); // Use Transformer to serialize a DOM TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // No need for pretty printing transformer.setOutputProperty(OutputKeys.INDENT, INDENT_XML); // XML Declarations unexpected whitespace for legacy AS XMLDocument type, // so we always omit it. We can't tell whether one was present when // constructing the Document in the first place anyway... transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION); transformer.transform(dom, output); xml = writer.toString(); } catch (TransformerException te) { throw new IOException("Error serializing Document as String: " + te.getMessageAndLocation()); } return xml; }
From source file:Main.java
public static void writeDocument(Document document, String location) { DOMSource source = new DOMSource(document); File file = new File(location); Result result = new StreamResult(file); Transformer xformer; try {//from ww w .j a va 2s. c o m xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static String prettyPrint(File file) { Transformer tf; try {/* w ww. j a v a2 s.c o m*/ tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult stringResult = new StreamResult(new StringWriter()); tf.transform(new DOMSource(convertFileToDom(file)), stringResult); return stringResult.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Parses {@code org.w3c.dom.Document} and returns its xml serialization as {@code String} * @param dom/* ww w . j a v a 2 s .co m*/ * @return */ public static String DocumentToString(Document dom) { String xmlString = null; try { 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(dom); transformer.transform(source, result); xmlString = result.getWriter().toString(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return xmlString; }
From source file:gov.nih.nci.cacis.common.test.TestUtils.java
/** * Will get an w3c Node element as a String * * @param node XML Node/*from www.jav a 2s. com*/ * @return String xml as String * @throws javax.xml.transform.TransformerException exception */ public static String nodeToString(Node node) throws TransformerException { final StringWriter sw = new StringWriter(); final Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:org.openmrs.module.metadatasharing.converter.ConverterEngine.java
private static String toXML(Node doc) throws SerializationException { try {//from w w w. j av a 2s . c om Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(new DOMSource(doc), result); return result.getWriter().toString(); } catch (Exception e) { throw new SerializationException(e); } }