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 StringBuffer transfer(Element element, StreamSource source) { try {// w ww.ja va 2 s . c o m StringWriter sw = new StringWriter(); Transformer trans = null; if (source != null) trans = TransformerFactory.newInstance().newTransformer(source); else trans = TransformerFactory.newInstance().newTransformer(); trans.transform(new DOMSource(element), new StreamResult(sw)); sw.close(); return sw.getBuffer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** Returns the entire value of a node (child nodes and text) as a String * @param node//from w w w .j a v a 2s . c o 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)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString().trim(); }
From source file:Main.java
public static NodeList selectNodeList(Node context, Transformer t) throws Exception { DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument() .createDocumentFragment();/*from w ww . j a va 2 s .co m*/ Result result = new DOMResult(df); t.transform(new DOMSource(context), result); return df.getChildNodes(); }
From source file:Main.java
public static void writeDocument(Document document, File directoryToWriteTo, String fileName) { // Prepare the DOM document for writing Source source = new DOMSource(document); // Prepare the output file File file = new File(directoryToWriteTo, fileName); Result result = new StreamResult(file); // Write the DOM document to the file try {//from w ww. j av a2 s. c om Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); } catch (TransformerFactoryConfigurationError | TransformerException e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {//from w w w. j a va 2s . c o m Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void saveXML(Document paramDocument, File paramFile) throws TransformerException { TransformerFactory localTransformerFactory = TransformerFactory.newInstance(); Transformer localTransformer = localTransformerFactory.newTransformer(); localTransformer.setOutputProperty("indent", "yes"); StreamResult localStreamResult = new StreamResult(paramFile); DOMSource localDOMSource = new DOMSource(paramDocument); localTransformer.transform(localDOMSource, localStreamResult); }
From source file:it.cnr.icar.eric.common.cms.AbstractService.java
protected static void printNodeToConsole(Node n) { try {//from w w w . j av a 2 s . c o m TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // also print to console transformer.transform(new DOMSource(n), new StreamResult(System.err)); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static String document2String(Document document, String encode) throws TransformerException { String xml = null;// www . j a v a 2s. c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); transformer.transform(source, new StreamResult(sw)); xml = sw.toString(); return xml; }
From source file:Main.java
public static void writeXmlDocToStream(Document xmlReport, OutputStream stream) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(xmlReport); StreamResult result = new StreamResult(stream); transformer.transform(source, result); }
From source file:Main.java
/** Returns the entier value of a node (child nodes and text) as a String * @param node/*ww w .jav a2 s . c o 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(); }