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 Document xslt(InputStream stylesheet, Document input) throws FileNotFoundException, TransformerException, ParserConfigurationException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet)); Document result = newDocument(); DOMResult domResult = new DOMResult(result); transformer.transform(new DOMSource(input), domResult); return result; }
From source file:Main.java
public static String xmlToString(Document doc) { try {//from w ww . j a v a 2 s .com TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; } catch (TransformerException exc) { throw new RuntimeException("Unable to convert XML to String"); } }
From source file:Main.java
public static void write(Element root, String xmlPath) { try {// w w w . ja va 2s . c o m Document doc = root.getOwnerDocument(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("encoding", "utf-8"); FileWriter fw = new FileWriter(xmlPath); transformer.transform(new DOMSource(doc), new StreamResult(fw)); fw.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String toString(Node element) throws Exception { if (element == null) { return "null"; }// w w w. j a v a 2s.c o m Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { Result result = new StreamResult(printWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } return stringWriter.toString(); }
From source file:Main.java
public static String writeXMLtoString(org.w3c.dom.Document doc) throws Exception { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); String xmlString = sw.toString(); return xmlString; }
From source file:Main.java
public static String transform2String(Node node) throws TransformerException { final TransformerFactory transFactory = TransformerFactory.newInstance(); final Transformer transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); return buffer.toString(); }
From source file:Main.java
public static String asString(Document doc) { try {// w ww . j ava 2s .c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (TransformerException e) { throw Throwables.propagate(e); } }
From source file:Main.java
/** * Clones a document object./*www. ja v a2s . c om*/ * * @param doc The document to be cloned. * @return The new document object that contains the same data as the original document. * @throws TransformerException Thrown if the document can't be */ public static Document cloneDocument(final Document doc) throws TransformerException { final Node rootNode = doc.getDocumentElement(); // Copy the doctype and xml version type data final TransformerFactory tfactory = TransformerFactory.newInstance(); final Transformer tx = tfactory.newTransformer(); final DOMSource source = new DOMSource(doc); final DOMResult result = new DOMResult(); tx.transform(source, result); // Copy the actual content into the new document final Document copy = (Document) result.getNode(); copy.removeChild(copy.getDocumentElement()); final Node copyRootNode = copy.importNode(rootNode, true); copy.appendChild(copyRootNode); return copy; }
From source file:Main.java
public static synchronized void createIdStorage() throws ParserConfigurationException, TransformerException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(IDS); doc.appendChild(rootElement);/*from w w w . j av a 2s .c o m*/ Transformer transformer = getTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(ID_STORAGE_LOCATION)); transformer.transform(source, result); }
From source file:Main.java
/** * @param xmlSource/*from w ww. ja va 2s . co m*/ * @param xslSource * @param resolver * @param output * @throws TransformerFactoryConfigurationError * @throws TransformerConfigurationException * @throws TransformerException */ public static void formatXML(Source xmlSource, Source xslSource, URIResolver resolver, Result result) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Templates t = factory.newTemplates(xslSource); Transformer transformer = t.newTransformer(); if (resolver != null) { transformer.setURIResolver(resolver); } transformer.transform(xmlSource, result); }