List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
/** * DOM to string.//from w ww . j av a 2s . com * * @param doc * the doc * @return the string */ /* * from: * http://www.journaldev.com/71/utility-java-class-to-format-xml-document * -to-xml-string-and-xml-to-document */ public static String DOMToString(Document doc) { String xmlString = ""; if (doc != null) { try { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); xmlString = sw.toString(); } catch (Exception e) { e.printStackTrace(); } } return xmlString; }
From source file:Main.java
/** * output XML data in a file//from w w w.j a va 2 s .com * * @param doc * @param path */ public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try { Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.transform(source, result); } catch (TransformerConfigurationException e) { //TODO } catch (TransformerException e) { } }
From source file:Main.java
public static String getXMLString(Node doc) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
public static String formatXmlAsStringNoWhitespace(Document document) { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); try {//from w w w . j av a 2 s . co m Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException ex) { throw new RuntimeException("Error formatting xml as no-whitespace string", ex); } return writer.toString(); }
From source file:Main.java
public static void addApp(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("app"); if (nodeList != null && nodeList.getLength() >= 1) { Node deviceNode = nodeList.item(0); Element device = doc.createElement("aut"); device.setTextContent(name);// w w w. j av a 2 s. com 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); }
From source file:Main.java
public static void writeXmlDocToStream(Document xmlReport, OutputStream stream) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(xmlReport); StreamResult result = new StreamResult(stream); transformer.transform(source, result); }
From source file:Main.java
public static byte[] serializeDocument(Document root) throws TransformerException, UnsupportedEncodingException { Source source = new DOMSource((Node) root); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); return serializeXML(transformer, source); }
From source file:Main.java
public static ByteArrayOutputStream printDOMDocumentToOutputStream(Document doc) { ByteArrayOutputStream os;// w ww. jav a 2 s .co m os = new ByteArrayOutputStream(); try { // TODO remove, nonsence here XMLSignatureHelper.storeSignatureToXMLFile(doc, "signature.xml"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); //trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); //trans.setOutputProperty(OutputKeys.INDENT, "yes"); //trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.transform(new DOMSource(doc), new StreamResult(os)); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException 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(); } return os; }
From source file:Main.java
public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {// ww w .j a v a2 s . c om Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void buildXmlFile(Document doc, File file) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {//from www . ja v a2 s . c o m Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }