List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static void encodeAsXml(Node dom, OutputStream os) throws Throwable { try {//from www . ja v a 2 s. c o m DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(os); Transformer transformer; transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (TransformerConfigurationException tce) { Throwable x = tce; if (tce.getException() != null) x = tce.getException(); throw x; } catch (TransformerException te) { Throwable x = te; if (te.getException() != null) x = te.getException(); throw x; } }
From source file:Main.java
public static void writeDocToFile(Document doc, String filename) throws Exception { Source source = new DOMSource(doc); File file = new File(filename); Result result = new StreamResult(file); TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", 2); Transformer xformer = tFactory.newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); xformer.transform(source, result);//w w w.ja va 2 s. c om /*OutputFormat format = new OutputFormat(doc); format.setIndenting(true); format.setIndent(2); Writer output = new BufferedWriter( new FileWriter(filename) ); XMLSerializer serializer = new XMLSerializer(output, format); serializer.serialize(doc);*/ }
From source file:Main.java
private static void finalizeXML(File xmlFile, Document doc, int intendAmount) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(xmlFile); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + intendAmount); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, result); }
From source file:Main.java
public static String xmlToString(Document xml) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try {/*ww w. j a v a2s.c o m*/ transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } DOMSource source = new DOMSource(xml); StringWriter writer = new StringWriter(); try { transformer.transform(source, new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } return writer.toString(); }
From source file:Main.java
public static String getXml(Document doc) { DOMSource doms = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); String xml = null;// w w w . j a v a 2s .c om try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "GB2312"); properties.setProperty(OutputKeys.METHOD, "xml");//! properties.setProperty(OutputKeys.VERSION, "1.0"); properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperties(properties); t.transform(doms, sr); String dtd = doc.getDoctype().getInternalSubset(); if ((null != dtd) && (dtd.length() > 0)) { dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n"; } ; xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd; xml += sw.toString(); } catch (TransformerConfigurationException tce) { //"Transformer Configuration Exception\n-----" } catch (TransformerException te) { //"Transformer Exception\n---------" } return xml; }
From source file:Main.java
public static void saveDoc(Document docToSave, String filePath) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(docToSave); StreamResult result = new StreamResult(new File(filePath)); transformer.transform(source, result); }
From source file:Main.java
public static void serialize(Document doc, OutputStream out) throws Exception { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;//from w ww .ja va 2 s. com try { serializer = tfactory.newTransformer(); //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(new DOMSource(doc), new StreamResult(out)); } catch (TransformerException e) { // this is fatal, just dump the stack and throw a runtime exception e.printStackTrace(); throw new RuntimeException(e); } }
From source file:Main.java
public static String encodeBase64(Element elm) throws Exception { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); TransformerFactory transFac = TransformerFactory.newInstance(); Transformer trans = transFac.newTransformer(); trans.transform(new DOMSource(elm), new StreamResult(ostream)); return (new String(Base64.encodeBase64(ostream.toByteArray(), true))); }
From source file:Main.java
public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {// ww w .ja v a 2 s. c o m Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { throw new RuntimeException(te); } return sw.toString(); }
From source file:Main.java
public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {//from ww w . j a v a2s. c om Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.err.println("nodeToString Transformer Exception"); } return sw.toString(); }