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
/** * convert an XML node to an XML statement * @param node current XML node//from w w w .j a v a 2 s . c o m * @return XML string */ public static String nodeToString(Node node) { //MagicBooleans.trace("nodeToString(node: " + node + ")"); StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } String result = sw.toString(); //MagicBooleans.trace("nodeToString() returning: " + result); return result; }
From source file:Main.java
public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException { /* TODO: Certain HTML entities, like "⇒", currently seem impossible to represent, as the HTML mode will output them in a named form unsupported by the Swing HTML parser (e.g. "⇐"). *///w ww .ja v a2 s . c o m Transformer t; t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no"); StringWriter ret = new StringWriter(); t.transform(new DOMSource(elm), new StreamResult(ret)); return ret.toString(); }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static String documentToString(Document doc, String xslName) { StringWriter sw = new StringWriter(); try {//from w w w. j a va2s. co m TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc.getDocumentElement()); Result result = new StreamResult(sw); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } return head + xslName + sw.toString(); }
From source file:Main.java
/** * Transform xml Document to String/*from w w w.j a v a2 s .c om*/ * @param doc Xml Document * @return String representation */ public static String getContent(Document doc) { try { StringWriter sw = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }
From source file:Main.java
public static void saveReportToFile(Node node, String documentFileName) { // StringWriter writer = new StringWriter(); // StreamResult resultString = new StreamResult(writer); File output = new File(documentFileName); StreamResult resultFile = new StreamResult(output); DOMSource source = new DOMSource(node); try {//w w w . j a v a2 s .c o m Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(source, resultFile); // t.transform(source, resultString); } catch (TransformerException e) { e.printStackTrace(); } // return writer.toString(); }
From source file:Main.java
public static String format(String input, int indent) { try {/*www .j a v a2 s . c o m*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void printDocument(Document doc) { try {// w w w .j av a 2 s . c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8"))); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String getDocumentAsXml(Document doc) { StringWriter sw = new java.io.StringWriter(); try {/*from w w w . ja va 2s . com*/ DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
/** * Transform xml Document to String//from w w w . j a v a 2 s. c o m * @param doc Xml Document * @return String representation */ public static String getContent(final Document doc) { try { final StringWriter sw = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }