List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:Main.java
private static byte[] serializeXML(Transformer transformer, Source input) throws UnsupportedEncodingException, TransformerException { StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); transformer.transform(input, result); String streamString = stringWriter.getBuffer().toString(); byte[] byteArray = streamString.getBytes("UTF-8"); return byteArray; }
From source file:Main.java
/** * Creates XML from W3C Document from the xml. * * @param document the xml that needs to be converted. * @return the XML./*from ww w. j a v a 2 s .c o m*/ * @throws Exception is there is some error during operation. */ public static String createXml(Document document) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter stringWriter = new StringWriter(); StreamResult result = new StreamResult(stringWriter); DOMSource source = new DOMSource(document); transformer.transform(source, result); return stringWriter.toString(); }
From source file:Main.java
public static synchronized void writeXmlFile(String xmlFile, Document document) throws Exception { // transform the document to the file TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer idTransformer = xformFactory.newTransformer(); idTransformer.setOutputProperty("indent", "yes"); Source input = new DOMSource(document); Result output = new StreamResult(new File(xmlFile)); idTransformer.transform(input, output); /*/*from www.j a va 2 s.c o m*/ OutputFormat format = new OutputFormat(); format.setLineSeparator(LineSeparator.Unix); format.setIndenting(true); format.setLineWidth(0); format.setPreserveSpace(true); XMLSerializer ser = new XMLSerializer(new FileWriter(xmlFile), format); ser.asDOMSerializer(); ser.serialize(document); */ return; }
From source file:Main.java
public static String getDOMString(Document doc) { String s = null;//from w w w. j a v a2s . c om final TransformerFactory tfactory = TransformerFactory.newInstance(); try { final Transformer xform = tfactory.newTransformer(); final Source src = new DOMSource(doc); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); xform.transform(src, result); s = writer.toString(); } catch (final Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
From source file:Main.java
public static String prettyPrint(DOMSource domSource) { try {//from w w w .j av a2 s.com Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(domSource, streamResult); return streamResult.getWriter().toString(); } catch (Exception e) { throw new RuntimeException("Error while pretty printing xml", e); } }
From source file:Main.java
/** * Convert a DOM tree into a String using transform * @param domDoc DOM object * @throws java.io.IOException I/O exception * @return XML as String *//*from w ww . ja va 2s . c om*/ public static String docToString2(Document domDoc) throws IOException { try { TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); trans.transform(new DOMSource(domDoc), result); return sw.toString(); } catch (Exception ex) { throw new IOException(String.format("Error converting from doc to string %s", ex.getMessage())); } }
From source file:Main.java
/** Writes the given DOM to the specified output stream. */ public static void writeXML(final OutputStream os, final Document doc) { try {//from www.j a v a 2 s. c o m final TransformerFactory transformFactory = TransformerFactory.newInstance(); final Transformer idTransform = transformFactory.newTransformer(); final Source input = new DOMSource(doc); final Result output = new StreamResult(os); idTransform.transform(input, output); } catch (final TransformerException exc) { exc.printStackTrace(); } // append newline to end of output try { os.write(System.getProperty("line.separator").getBytes()); } catch (final IOException exc) { exc.printStackTrace(); } }
From source file:Main.java
public static String nodeToString(Node node) { try {//w w w.j a v a 2 s . c o m Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(node); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); return writer.toString(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
/** * Returns the XML string representation of a Node. * @param node Node to convert into XML string. * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. * @return The XML string representation of the input node. * @throws TransformerConfigurationException * @throws TransformerException /*from w w w .jav a 2s.com*/ */ public static String getXMLString(Node node, boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerException { StringWriter writer = new StringWriter(); DOMSource domSource = new DOMSource(node); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); serializer.transform(domSource, result); return (writer.toString()); }
From source file:Main.java
/** * This class converts a DOM representation node to text. * @param node/*from www . ja v a 2 s . co m*/ * @return * @throws Exception */ public static final String toText(Node node) throws Exception { String ret = null; try { // Transform the DOM represent to text ByteArrayOutputStream xmlstr = new ByteArrayOutputStream(); DOMSource source = new DOMSource(node); //source.setNode(node); StreamResult result = new StreamResult(xmlstr); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.transform(source, result); xmlstr.close(); ret = new String(xmlstr.toByteArray()); if ((node instanceof Document) == false) { // Strip off any <?xml> header int index = ret.indexOf("<?xml"); if (index != -1) { index = ret.indexOf("<", 1); if (index != -1) { ret = ret.substring(index); } else { index = ret.indexOf("?>"); if (index != -1) { index += 2; // Remove any trailing whitespaces after XML header while (index < ret.length() && Character.isWhitespace(ret.charAt(index))) { index++; } ret = ret.substring(index); } } } } } catch (Exception e) { throw new Exception("Failed to transform DOM representation into text", e); } if (ret != null) { return format(ret); } return ret; }