List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
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;//from ww w. j a v a 2 s .co m 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 String convertDocumentToString(Document doc) throws TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Unquote below to remove XML declaration. // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
public static String documentToString(Document document) throws TransformerException { Node rootNode = (Node) document.getDocumentElement(); Source source = new DOMSource(rootNode); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString().replace("\n", ""); }
From source file:Main.java
public static void SaveDom(Document dom, String filepath) { try {/*w w w . j a v a 2s . c om*/ TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(dom); StreamResult streamResult = new StreamResult(new FileOutputStream(filepath)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.transform(domSource, streamResult); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void saveDocument(Document dom, String file) throws TransformerException, IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dom.getDoctype().getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dom.getDoctype().getSystemId()); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(); FileOutputStream outputStream = null; try {//from ww w . j a va2s . c om outputStream = new FileOutputStream(file); result.setOutputStream(outputStream); transformer.transform(source, result); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } }
From source file:Main.java
/** * Serialize W3C document into given output stream * @param doc W3C document/*from w w w . ja v a 2 s. c om*/ * @param out OutputStream * @throws TransformerException */ public static void print(Document doc, OutputStream out) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(out); transformer.transform(source, result); }
From source file:Main.java
public static synchronized void writeXmlFile(String xmlFile, Document document) throws Exception { // transform the document to the file TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer idTransformer = xformFactory.newTransformer(); idTransformer.setOutputProperty("indent", "yes"); Source input = new DOMSource(document); Result output = new StreamResult(new File(xmlFile)); idTransformer.transform(input, output); /*//from ww w . jav a 2 s .c om OutputFormat format = new OutputFormat(); format.setLineSeparator(LineSeparator.Unix); format.setIndenting(true); format.setLineWidth(0); format.setPreserveSpace(true); XMLSerializer ser = new XMLSerializer(new FileWriter(xmlFile), format); ser.asDOMSerializer(); ser.serialize(document); */ return; }
From source file:Main.java
public static String documentToString(Document doc, String xslName) { StringWriter sw = new StringWriter(); try {//from w w w . java2s . c o m TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc.getDocumentElement()); Result result = new StreamResult(sw); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } return head + xslName + sw.toString(); }
From source file:Main.java
public static String createPrintableString(Document doc) { String s = ""; try {//from w ww .j a v a 2 s. c om // set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); s = sw.toString(); } catch (Exception e) { e.printStackTrace(); } return s; }
From source file:Main.java
private static void SaveCustomerFile(Document CustomerDoc, JFrame mainFrame) { try {//from w ww . ja v a 2 s. co m TransformerFactory Factory = TransformerFactory.newInstance(); Transformer Trans = Factory.newTransformer(); DOMSource source = new DOMSource(CustomerDoc); File f = new File("."); String FilePath = f.getAbsoluteFile().getParent() + "\\Customers.xml"; f = new File(FilePath); if (f.exists()) { f.delete(); } StreamResult Result = new StreamResult(f); Trans.setOutputProperty(OutputKeys.INDENT, "yes"); Trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "7"); Trans.transform(source, Result); } catch (TransformerException ex) { System.out.println(ex.getMessage()); JOptionPane.showMessageDialog(mainFrame, "There Was an Error Saving The File. Please Restart the Application."); System.exit(1); } }