List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static Transformer getTransformer() throws TransformerConfigurationException { TransformerFactory tf = TransformerFactory.newInstance(); return tf.newTransformer(); }
From source file:Main.java
public static String convertXMLFileToString(String fileName) { try {//from w ww. j a v a 2 s .c om DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(stw)); return stw.toString(); } catch (Exception e) { System.out.println("Unable to load xml file: " + e.toString()); } return null; }
From source file:Main.java
public static void ElementToStream(Element element, OutputStream out) { try {//from www . j a va 2 s. co m DOMSource source = new DOMSource(element); StreamResult result = new StreamResult(out); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.transform(source, result); } catch (Exception ex) { } }
From source file:Main.java
public static void writeDocument(Document document, String location) { DOMSource source = new DOMSource(document); File file = new File(location); Result result = new StreamResult(file); Transformer xformer;//from w ww . j a va 2 s . co m try { xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static void format(Source source, boolean omitDeclaration, Result result) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); try {/*from w ww.ja v a 2s . com*/ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (IllegalArgumentException exc) { // not supported } if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); }
From source file:Main.java
/** * Convenience method to get a new instance of a TransformerFactory. * If the {@link #TransformerFactory} is an instance of * net.sf.saxon.TransformerFactoryImpl, the attribute * {@link #FeatureKeys.VERSION_WARNING} will be set to false in order to * suppress the warning about using an XSLT1 stylesheet with an XSLT2 * processor./* w w w . ja v a2s . c o m*/ * * @return a new instance of TransformerFactory */ public static TransformerFactory getTransformerFactory() { TransformerFactory factory = TransformerFactory.newInstance(); if (factory.getClass().getName().equals("net.sf.saxon.TransformerFactoryImpl")) { factory.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE); } return factory; }
From source file:Main.java
/** * Updates existing file with Document object * //from w ww. ja v a2 s .c o m * @param doc * xml document * @param file * xml file to update * @throws TransformerFactoryConfigurationError */ public static void updateXMLFile(Document doc, File file) throws TransformerFactoryConfigurationError { try { DOMSource source = new DOMSource(doc); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException("Exception while saving message to xml file", e); } }
From source file:Main.java
public static void style(Reader xsl, Reader xml, Writer out, Map params) throws TransformerException { Source xmlSource = new javax.xml.transform.stream.StreamSource(xml); TransformerFactory factory = TransformerFactory.newInstance(); Source xslSource = new javax.xml.transform.stream.StreamSource(xsl); Transformer transformer;/*ww w . j a v a 2 s .c o m*/ transformer = factory.newTransformer(xslSource); if (params != null && !params.isEmpty()) { Iterator entries = params.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); transformer.setParameter((String) entry.getKey(), entry.getValue()); } } StreamResult result = new StreamResult(out); transformer.transform(xmlSource, result); }
From source file:Main.java
public static boolean ExportXML(Document xmlOutput, String filename) { try {/*from w w w. j ava 2 s . c om*/ if (xmlOutput != null) { Source source = new DOMSource(xmlOutput); FileOutputStream file = new FileOutputStream(filename); StreamResult res = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, res); file.close(); return true; } return false; } catch (FileNotFoundException e) { return false; } catch (TransformerConfigurationException e) { return false; } catch (TransformerException e) { return false; } catch (IOException e) { return false; } }
From source file:Main.java
/** * Transform a org.w3c.dom.Node to a String * @param node - the Node//from www . j a va2 s . co m * @param omitXmlDeclaration - omit XML declaration * @param prettyPrint - apply indentation * @return the String result * @throws TransformerException */ public static String elementToString(Node node, boolean omitXmlDeclaration, boolean prettyPrint) { String result = null; try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); if (omitXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } if (prettyPrint) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } transformer.transform(new DOMSource(node), new StreamResult(buffer)); result = buffer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } return result; }