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
/** * Method for static initializer/*from w ww . ja v a2s . co m*/ */ private static Properties createDefaultPropertiesForXML(boolean omitXMLDeclaration) { final Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); format.put(OutputKeys.OMIT_XML_DECLARATION, (omitXMLDeclaration ? "yes" : "no")); format.put(OutputKeys.INDENT, "yes"); return format; }
From source file:Main.java
/** * Convert XML DOM document to a XML string representation * * @param doc//from w w w. j a v a 2s. c o m * 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
public static String generateXml() { DocumentBuilder documentBuilder = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from w ww . jav a 2 s . co m documentBuilder = docFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); DOMSource docSource = new DOMSource(document); StreamResult result = new StreamResult(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); result.setOutputStream(baos); transformer.transform(docSource, result); } catch (Exception e) { e.printStackTrace(); } return baos.toString(); }
From source file:Main.java
private static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void formatDocument(Document document, Writer out, String encoding) throws TransformerException { document.normalizeDocument();//from w w w . j ava 2 s . co m TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer idTransform = transFactory.newTransformer(); idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); idTransform.setOutputProperty(OutputKeys.INDENT, "yes"); if (encoding != null) { idTransform.setOutputProperty(OutputKeys.ENCODING, encoding); } idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source source = new DOMSource(document); Result result = new StreamResult(out); idTransform.transform(source, result); }
From source file:Main.java
/** * Pretty format a given XML document// w w w. ja va 2 s . c o m * * @param strInput * Valid XML document (No validity check yet!) * @param nIndent * Indent * @return Formatted XML document * @throws Exception * in error case */ public static String prettyFormat(String strInput, int nIndent) throws Exception { try { Source xmlInput = new StreamSource(new StringReader(strInput)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", nIndent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { // Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error); throw e; } }
From source file:Main.java
public static String getXml(Document doc) { try {/* www . ja v a 2s. c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = result.getWriter().toString(); return xmlString; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String serializeDocument(Document document) throws Exception { // Serialize XML document to String. StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); DOMSource domSource = new DOMSource(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return writer.toString(); }
From source file:Main.java
public static void printDOM(OutputStream ostr, Document doc, String encoding) { try {//from w w w . j av a 2 s . c om Transformer tr = TransformerFactory.newInstance().newTransformer(); if (encoding != null) tr.setOutputProperty(OutputKeys.ENCODING, encoding); tr.setOutputProperty(OutputKeys.INDENT, "yes"); tr.setOutputProperty(OutputKeys.METHOD, "xml"); tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); tr.transform(new DOMSource(doc), new StreamResult(ostr)); } catch (TransformerConfigurationException ex) { System.err.println(ex.getMessage()); } catch (TransformerException ex) { System.err.println(ex.getMessage()); } }
From source file:Main.java
private static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }