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 Transformer newTransformer(boolean indented) { String indentFlag = (indented) ? "yes" : "no"; try {//from w w w . j a v a 2 s .c o m TransformerFactory factory = TransformerFactory.newInstance(); factory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer; transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, indentFlag); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); return transformer; } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Get standard xml string of node// ww w.j a v a2 s .c o m * * @param node * @return */ public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try { 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)); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static String XMLDocumentToString(Document _doc) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans;//w ww . j a va 2 s .c om String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; try { 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 (TransformerConfigurationException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } catch (TransformerException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } return xmlString; }
From source file:Main.java
public static void serialize(Source source, Writer writer) throws Exception { serialize(source, writer, ImmutableMap.of(OutputKeys.ENCODING, "UTF-8", OutputKeys.INDENT, "yes")); }
From source file:Main.java
public static String DocToString(Document doc) throws TransformerFactoryConfigurationError, TransformerException { if (doc == null) return new String(""); String xmlString = ""; 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); xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String prettyFormat(String input, int indent, boolean isOmitXmlDeclaration) { try {/* w ww . j av a2s. 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"); if (isOmitXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
/** * @param xml/*from w ww .ja v a2 s . c om*/ * @return pretty xml */ public static String prettyXml(String xml) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { Source source = new StreamSource(new StringReader(xml)); StringWriter writer = new StringWriter(); serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (Exception e) { return xml; } }
From source file:Main.java
public static String formatDocument2(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return new String(out.toByteArray()); }
From source file:Main.java
/** * Returns the String-Representation of the given DOM-Node as well-formed DOM-Document. * * @param node DOM-Node to print// www . j a va 2s.c om * @param indent if true resulting XML is endented * @return <code>String</code> - Node as XML-String * @throws Exception on error */ public static String domNode2String(Node node, boolean indent) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
/** * write out an XML file/*from w ww .j a v a2 s . c o m*/ * * @param doc * @param os * @throws TransformerException * @throws IOException */ public static void writeXML(Document doc, OutputStream os) throws TransformerException, IOException { // write out xml file TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //indent XML properly //formatXML(doc,doc.getDocumentElement()," "); //normalize document doc.getDocumentElement().normalize(); //write XML to file DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(os); transformer.transform(source, result); os.close(); }