List of usage examples for javax.xml.transform OutputKeys INDENT
String INDENT
To view the source code for javax.xml.transform OutputKeys INDENT.
Click Source Link
From source file:Main.java
public static String prettyXML(String xml, int indent) { try {// w w w . jav a 2 s . c o m // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Convert the document to an array of bytes. * * @param doc The XML document./* ww w .j av a 2s. com*/ * @param encoding The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); try { return writer.getBuffer().toString().getBytes(encoding); } catch (UnsupportedEncodingException e) { throw new TransformerException("Unsupported Encoding", e); } }
From source file:Main.java
public static byte[] serializeToByteArray(Document doc) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try {/*w w w .j a v a 2 s. c om*/ transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); } transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); String encoding = doc.getInputEncoding(); if (encoding == null) encoding = "UTF-8"; transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } return out.toByteArray(); }
From source file:Main.java
public static String getString(Node doc) { StringWriter sw = new StringWriter(); try {/*from ww w . j av a 2s .c o m*/ 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 trans.transform(new DOMSource(doc), new StreamResult(sw)); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
/** * Format the provided XML input.//from w w w . ja v a 2 s. c om * * @param input * XML input to format. * @param indent * Indentation to use on formatted XML. * @return Formatted XML. * @throws TransformerException * if some problem occur while processing the xml * @see #prettyFormat(String) */ public static String prettyFormat(String input, Integer indent) throws TransformerException { if (input != null) { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent == null ? 2 : indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } return input; }
From source file:Main.java
/** * // ww w.ja v a 2 s. co m * <B>Purpose:</B> Writes the XML Document Object to a File * * @param doc * @param file * @throws TransformerException */ public static void writeXmlFile(Node doc, File file) throws TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); xformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n"); xformer.setOutputProperty("standalone", "yes"); xformer.transform(source, result); }
From source file:Main.java
public static void writeXML(String OUTPUT_XML_FILE, org.w3c.dom.Document xmlDoc) { try {//from w ww . j av a2 s . c o m javax.xml.transform.Source source = new DOMSource(xmlDoc); System.out.println("writing File: " + OUTPUT_XML_FILE); // Prepare the output file File file = new File(OUTPUT_XML_FILE); Result result = new StreamResult(file); // Write the DOM document to the file javax.xml.transform.Transformer xformer = javax.xml.transform.TransformerFactory.newInstance() .newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); xformer.transform(source, result); } catch (Exception e) { System.err.println("Exception in writing file:" + OUTPUT_XML_FILE); e.printStackTrace(); } }
From source file:Main.java
public static void writeXml(Document doc, OutputStream outputStream) { try {/* w ww .j a va 2s . com*/ // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file Result result = new StreamResult(outputStream); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); try { xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); } catch (IllegalArgumentException e) { // ignore } try { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); } catch (IllegalArgumentException e) { // ignore } xformer.transform(source, result); } catch (TransformerConfigurationException e) { throw new IllegalStateException(e); } catch (TransformerException e) { throw new IllegalStateException(e); } }
From source file:Main.java
static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null;//from ww w.j ava 2s. c o m try { t = tf.newTransformer(); t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.ENCODING, encoding); } catch (TransformerConfigurationException tce) { assert (false); } DOMSource doms = new DOMSource(doc); StreamResult sr = new StreamResult(os); try { t.transform(doms, sr); } catch (TransformerException te) { IOException ioe = new IOException(); ioe.initCause(te); throw ioe; } }
From source file:Main.java
public static String tryFormattingString(String s) { try {/* ww w . j a v a 2 s. c o m*/ ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { } @Override public void fatalError(SAXParseException exception) throws SAXException { } @Override public void error(SAXParseException exception) throws SAXException { } }); Document doc = builder.parse(in); TransformerFactory tf = TransformerFactory.newInstance(); // tf.setAttribute("indent-number", new Integer(2)); Transformer trans = tf.newTransformer(); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult result = new StreamResult(out); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.transform(source, result); return out.toString(); } catch (Exception e) { // Console.println("Could not validate the soapmessage, although I'll show it anyway.."); } return s; }