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
public static OutputStream writeDocument(Document doc, OutputStream os) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer; try {// w w w . ja v a2s. c o m transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(os); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return os; }
From source file:Main.java
public static void saveXML(Document doc, OutputStream stream) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer; try {// ww w . ja va 2 s .c om transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(stream); transformer.transform(source, result); stream.flush(); stream.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * Writes an element out to a file.//w w w . j av a 2 s. c o m * @param element The XML element to write out. * @param fileName The file name to write to. Existing file is overwriten. */ public static void writeElement(Element element, String fileName) { File file = new File(fileName); file.delete(); DOMSource domSource = new DOMSource(element); FileOutputStream fos = null; try { fos = new FileOutputStream(file); StreamResult result = new StreamResult(fos); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(domSource, result); fos.flush(); fos.close(); } catch (Exception e) { throw new RuntimeException("Failed to write XML element to:" + fileName, e); } finally { try { fos.flush(); } catch (Exception e) { } try { fos.close(); } catch (Exception e) { } } }
From source file:Main.java
public static void document2OutputStream(Document document, String encode, OutputStream os) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, new StreamResult(os)); }
From source file:Main.java
public static String transferXmlToString(Document doc) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {// w w w .j a va 2s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties p = t.getOutputProperties(); p.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(p); t.transform(new DOMSource(doc), new StreamResult(bos)); } catch (NullPointerException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } String xmlStr = ""; try { xmlStr = bos.toString("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return xmlStr; }
From source file:Main.java
public static void printXml(Document xml, Writer out) throws TransformerException, UnsupportedEncodingException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(xml), new StreamResult(out)); }
From source file:Main.java
public static void sendXml(String result, int operationN) throws Exception { String filepath = "file.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(filepath); Node tonode = doc.getElementsByTagName("command").item(0); Element res = doc.createElement("result"); res.setTextContent(result);// w ww . j ava 2 s . c o m tonode.appendChild(res); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result1 = new StreamResult(new File("file2.xml")); StreamResult result3 = new StreamResult(System.out); transformer.transform(source, result1); }
From source file:scala.c24.demo.java.C24DemoUtils.java
public static String getDocumentAsString(Resource resource) throws Exception { Document document = getDocument(resource); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
public static String getStringFromElement(Element doc) throws TransformerException { 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"); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, result); writer.flush();//from w w w . j a va 2 s . co m return writer.toString(); }
From source file:Main.java
/** * returns an XML string.// ww w. jav a2 s . c om * * @param pNode Node XML Document node * @return String XML string */ public static String getXML(Node pNode) { String retString = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.transform(new DOMSource(pNode), new StreamResult(out)); retString = out.toString(); out.close(); } catch (Exception ex) { } return retString; }