List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
/** * Applies an XSL transformation to <code>xmlString</code> using * <code>xsltStr</code> as the stylesheet and returns the transformed * string./*from w ww . j a v a2s . com*/ * * @param xmlString * The XML to transform. * @param xsltStr * The stylesheet as an XML string (not a file). * @return Transformed string. * @throws Exception */ public static String applyStylesheetAsString(String xmlString, String xsltStr) throws Exception { // Use the static TransformerFactory.newInstance() method to instantiate // a TransformerFactory. The javax.xml.transform.TransformerFactory // system property setting determines the actual class to instantiate -- // org.apache.xalan.transformer.TransformerImpl. TransformerFactory tFactory = TransformerFactory.newInstance(); // Use the TransformerFactory to instantiate a Transformer that will work with // the stylesheet you specify. This method call also processes the // stylesheet into a compiled Templates object. Transformer transformer = tFactory .newTransformer(new StreamSource(new ByteArrayInputStream(xsltStr.getBytes()))); // Use the Transformer to apply the associated Templates object to an // XML document (foo.xml) and write the output to a file (foo.out). StringWriter swriter = new StringWriter(); StreamResult sresult = new StreamResult(swriter); transformer.transform(new StreamSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))), sresult); return swriter.toString(); }
From source file:Main.java
public static void printDOM(OutputStream ostr, Document doc, String encoding) { try {//from ww w. java 2s . c o 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
public static Transformer buildTransformer() throws TransformerConfigurationException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); return transformer; }
From source file:Main.java
public static void writeXMLDocument(Document doc, StreamResult out) throws TransformerConfigurationException, TransformerException { DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
public static String getXML(NodeList childNodes) { try {// w w w . jav a 2 s.c o m StringBuilder builder = new StringBuilder(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); for (int i = 0; i < childNodes.getLength(); i++) { StringWriter sw = new StringWriter(); t.transform(new DOMSource(childNodes.item(i)), new StreamResult(sw)); builder.append(sw.toString()); } return builder.toString(); } catch (Exception ex) { return ""; } }
From source file:Main.java
static public void outputToStream(Document document, OutputStream outputStream) throws TransformerException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); }
From source file:Main.java
/** * Pretty print an XML. Use with caution as this drains the source if it is * a StreamSource.//from w w w . ja v a2s . c om * @param source the XML source * @return a String with readable XML */ public static String prettyPrint(final Source source) { try { Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { return e.getMessage(); } }
From source file:Main.java
public static void createXML222(String xmlFile, String xpath, String element, String data) { try {//from w w w. ja v a 2 s . c o m DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(xpath); document.appendChild(rootElement); for (int i = 1; i <= 1; i++) { Element em = document.createElement(element); em.appendChild(document.createTextNode(data)); rootElement.appendChild(em); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
/** * Creates a string representation of a {@link Node} instance. This method * does not introduce any character to the string representation of the * {@link Node} (eg. \n or \r characters) * * @param node A {@link Node} instance/* w w w. j a v a 2s. c o m*/ * @return A string representation of the node instance * @throws RequestSecurityTokenException */ public static String xmlToString(Node node) throws Exception { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } catch (TransformerException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } }
From source file:Main.java
/** * Method to get formatted Xml string from Xml source * * @param payload Source/*from w w w.j a va 2 s . c o m*/ * @return a formatted Xml string */ public static String getXmlStringFromSource(Source payload) { String result = null; StreamResult strResult = new StreamResult(new StringWriter()); if (payload != null) { try { TransformerFactory factory = TransformerFactory.newInstance(); //factory.setAttribute("indent-number", new Integer(2)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); transformer.transform(payload, strResult); } catch (TransformerException e) { e.printStackTrace(); } result = strResult.getWriter().toString(); } return result; }