List of usage examples for javax.xml.transform Transformer setOutputProperties
public abstract void setOutputProperties(Properties oformat);
From source file:Main.java
public static String toXML(Document dom, Properties outputProperties) { try {//ww w . j a v a 2 s. c o m DOMSource source = new DOMSource(dom); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Transformer tx = TransformerFactory.newInstance().newTransformer(); if (outputProperties != null) { tx.setOutputProperties(outputProperties); } tx.transform(source, result); String s = writer.toString(); writer.close(); return s; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Converts a dom to a String/*w w w . j a v a 2 s . co m*/ * * @param dom * dom to convert * @param outputProperties * the properties for the String representation of the XML * @return the dom as a String */ public static String writeDomToString(Document dom, Properties outputProperties) { try { StringWriter ret = new StringWriter(); TransformerFactory transFact = TransformerFactory.newInstance(); // transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(); if (outputProperties != null) transformer.setOutputProperties(outputProperties); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(ret); transformer.transform(source, result); return ret.toString(); } catch (Exception e) { throw new RuntimeException("Could not write dom to string!", e); } }
From source file:Main.java
/** * Converts a dom to a String/*w ww . j a v a 2 s . c o m*/ * * @param dom dom to convert * @param outputProperties the properties for the String representation of * the XML * @return the dom as a String */ public static String writeDomToString(Document dom, Properties outputProperties) { try { StringWriter ret = new StringWriter(); TransformerFactory transFact = TransformerFactory.newInstance(); // transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(); if (outputProperties != null) { transformer.setOutputProperties(outputProperties); } DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(ret); transformer.transform(source, result); return ret.toString(); } catch (Exception e) { throw new RuntimeException("Could not write dom to string!", e); } }
From source file:Main.java
public static Node applyXslToDocument2(Source xslt, Source doc, URIResolver resolver, Properties transformerProperties, HashMap<String, String> params, String transformerClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = null; if (transformerClassName == null) transformerFactory = TransformerFactory.newInstance(); else {/*from ww w. j a va2 s . c o m*/ Class transformerClass = Class.forName(transformerClassName); Constructor defaultConstructor = transformerClass.getConstructor(null); transformerFactory = (TransformerFactory) transformerClass.newInstance(); } if (resolver != null) transformerFactory.setURIResolver(resolver); Transformer transformer = transformerFactory.newTransformer(xslt); if (transformerFactory != null) transformer.setOutputProperties(transformerProperties); if (params != null) { for (Map.Entry<String, String> cursor : params.entrySet()) { transformer.setParameter(cursor.getKey(), cursor.getValue()); } } DOMResult result = new DOMResult(); transformer.transform(doc, result); return (result.getNode()); }
From source file:Main.java
public static String applyXslToDocument(Source xslt, Source doc, URIResolver resolver, Properties transformerProperties, HashMap<String, String> params, String transformerClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = null; if (transformerClassName == null) transformerFactory = TransformerFactory.newInstance(); else {//from ww w . j a v a 2 s . c o m Class transformerClass = Class.forName(transformerClassName); Constructor defaultConstructor = transformerClass.getConstructor(null); transformerFactory = (TransformerFactory) transformerClass.newInstance(); } if (resolver != null) transformerFactory.setURIResolver(resolver); Transformer transformer = transformerFactory.newTransformer(xslt); if (transformerFactory != null) transformer.setOutputProperties(transformerProperties); if (params != null) { for (Map.Entry<String, String> cursor : params.entrySet()) { transformer.setParameter(cursor.getKey(), cursor.getValue()); } } StringWriter strWriter = new StringWriter(); StreamResult result = new StreamResult(strWriter); transformer.transform(doc, result); return (strWriter.toString()); }
From source file:Main.java
public static String transferXmlToString(Document doc) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {// w w w . j a v a 2s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties p = t.getOutputProperties(); p.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(p); t.transform(new DOMSource(doc), new StreamResult(bos)); } catch (NullPointerException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } String xmlStr = ""; try { xmlStr = bos.toString("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return xmlStr; }
From source file:Main.java
public static void writeDomToFile(Document dom, File outFile, Properties outputProperties) { // try { // StringWriter ret = new StringWriter(); // TransformerFactory transFact = TransformerFactory.newInstance(); //// transFact.setAttribute("indent-number", 2); // Transformer transformer = transFact.newTransformer(); // if (outputProperties != null) transformer.setOutputProperties(outputProperties); // DOMSource source = new DOMSource(dom); // StreamResult result = new StreamResult(ret); try {//from w w w . j a v a 2 s .c o m TransformerFactory transFact = TransformerFactory.newInstance(); Transformer transformer = transFact.newTransformer(); if (outputProperties != null) transformer.setOutputProperties(outputProperties); DOMSource source = new DOMSource(dom); StreamResult result; result = new StreamResult( new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding"))); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException( "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e); } }
From source file:Main.java
public static void removeHandset(String file, String name) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList devices = doc.getElementsByTagName("devices"); NodeList nodeList = doc.getElementsByTagName("device"); for (int i = 0; i < nodeList.getLength(); i++) { Node deviceNode = nodeList.item(i); if (deviceNode.getTextContent().equals(name)) { devices.item(0).removeChild(deviceNode); }/* www .ja va2 s. co m*/ } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }
From source file:Main.java
private static boolean writeTo(Document doc, String fileName) throws Exception { boolean isOver = false; DOMSource doms = new DOMSource(doc); File f = new File(fileName); StreamResult sr = new StreamResult(f); try {// w w w .ja v a2 s.com TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(properties); t.transform(doms, sr); isOver = true; } catch (TransformerConfigurationException tce) { tce.printStackTrace(); } catch (TransformerException te) { te.printStackTrace(); } return isOver; }
From source file:Main.java
public static void addHandset(String file, String name, Hashtable<String, String> attri) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList nodeList = doc.getElementsByTagName("devices"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("device"); device.setTextContent(name);/* ww w . jav a 2 s.co m*/ for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) { String attriKey = (String) itrName.next(); String attriValue = (String) attri.get(attriKey); device.setAttribute(attriKey, attriValue); } deviceNode.appendChild(device); } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); }