List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Main.java
/** * Transform this {@link Node} into a {@link String} representation. * * @param xml// w w w .j ava 2 s.c o m * the xml Node * @return a String representation of the given xml Node */ public static String toString(Node xml) { short type = xml.getNodeType(); if (type == Node.TEXT_NODE) return xml.getNodeValue(); StringWriter buffer = new StringWriter(); try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(xml), new StreamResult(buffer)); } catch (IllegalArgumentException | TransformerException e) { //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'."); return ""; } return buffer.toString(); }
From source file:Main.java
/** * a debug method//from w ww . ja v a2s . com * * @param node A node to be dumped to a string * @param omitDeclaration A boolean whether to omit the XML declaration * @return A string representation of the node. * @throws Exception If anything goes wrong. Error handling omitted. */ public static String dumpNode(Node node, boolean omitDeclaration) throws Exception { Transformer xformer = TransformerFactory.newInstance().newTransformer(); if (omitDeclaration) { xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Source source = new DOMSource(node); xformer.transform(source, result); return sw.toString(); }
From source file:Main.java
/** * Writes the given document to the given writer. * * @param document the document to write * @param writer receives the written document * @param indent number of spaces to indent, null means don't indent *///w w w . ja v a2 s .c om public static void writeDocument(Document document, Writer writer, Integer indent) { TransformerFactory tf = TransformerFactory.newInstance(); try { Transformer trans = tf.newTransformer(); if (indent != null) { trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent)); } trans.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String nodeListToString(NodeList nodeList) throws TransformerException { StringWriter stringWriter = new StringWriter(); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); if (node instanceof Element) { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(stringWriter)); } else {/* w w w . ja v a 2 s.c om*/ stringWriter.append(node.getTextContent()); } } return stringWriter.toString(); }
From source file:Main.java
/** * /*from w w w.j a va 2 s .co m*/ * <B>Purpose:</B>Converts an XML Document to a String Object * * @param node * @return * @throws TransformerException */ public static String xmlToString(Node node) throws TransformerException { if (node == null) { return ""; } Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n"); transformer.setOutputProperty("omit-xml-declaration", "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); }
From source file:Main.java
/** * Convert XML DOM document to a XML string representation * * @param doc//from ww w .j av a2 s. com * XML DOM document * @return XML string * @throws Exception * in error case */ public static String xmlDOMDocumentToString(Document doc) throws Exception { if (doc == null) { throw new RuntimeException("No XML DOM document (null)!"); } StringWriter stringWriter = new StringWriter(); String strDoc = null; try { StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); // transformerFactory.setAttribute("nIndent-number", new Integer(4)); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult); stringWriter.flush(); strDoc = stringWriter.toString(); } catch (Exception e) { // Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (stringWriter != null) { stringWriter.close(); stringWriter = null; } } return strDoc; }
From source file:Main.java
/** * Writes the XML document to the particular file specified as argument * /*from w w w . j a v a 2 s.c o m*/ * @param doc the document * @param filename the path to the file in which to write the XML data writing * operation fails */ public static void writeXMLDocument(Document doc, String filename) { try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); log.fine("writing operation to " + filename + " successful!"); } catch (TransformerConfigurationException e) { log.warning(e.getMessage()); } catch (TransformerException e) { log.warning(e.getMessage()); } }
From source file:Main.java
public static boolean ExportXML(Document xmlOutput, String filename) { try {//from w ww .j ava 2 s. c o m 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
public static String toString(Node element) throws Exception { if (element == null) { return "null"; }// w w w . j a v a 2 s .c o m Source source = new DOMSource(element); StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { Result result = new StreamResult(printWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } return stringWriter.toString(); }
From source file:Main.java
public static void encodeAsXml(Node dom, OutputStream os) throws Throwable { try {/* w ww .j ava 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; } }