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 void print(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", "2"); transformer.transform(new DOMSource(this.doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:eu.eidas.engine.test.simple.SSETestUtils.java
/** * Prints the tree DOM.//from ww w .j a v a2 s . c o m * * @param samlToken the SAML token * @param isIndent the is indent * * @return the string * @throws TransformerException the exception */ public static String printTreeDOM(final Element samlToken, final boolean isIndent) throws TransformerException { // set up a transformer final TransformerFactory transfac = TransformerFactory.newInstance(); final Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, String.valueOf(isIndent)); // create string from XML tree final StringWriter stringWriter = new StringWriter(); final StreamResult result = new StreamResult(stringWriter); final DOMSource source = new DOMSource(samlToken); trans.transform(source, result); final String xmlString = stringWriter.toString(); return xmlString; }
From source file:Main.java
public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id) throws Exception { DocumentBuilder b = documentBuilder(); Document doc = b.newDocument(); String str = null;//from w w w. j a v a2s .co m SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Element root = doc.createElement(root_elem_id); doc.appendChild(root); // Now, add all the children for (Entry<String, String> e : content.entrySet()) { Element item = doc.createElement(item_elem_id); item.setAttribute("id", e.getKey()); CDATASection data = doc.createCDATASection(e.getValue()); item.appendChild(data); root.appendChild(item); } try { DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); TransformerHandler th = tf.newTransformerHandler(); th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); // format.put("{http://xml. customer .org/xslt}indent-amount", "4"); // format.put("indent-amount", "4"); // format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); th.getTransformer().setOutputProperties(format); th.setResult(sr); th.getTransformer().transform(ds, sr); str = out.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:Main.java
public static void writeXmlDocToStream(Document xmlReport, OutputStream stream) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(xmlReport); StreamResult result = new StreamResult(stream); transformer.transform(source, result); }
From source file:Main.java
/** * @param indentation If greater than zero, then the XML will be indented by the value specified *///from w w w . j a v a 2 s . co m private static Transformer getXMLidentityTransformer(int indentation) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer t = factory.newTransformer(); // identity transform t.setOutputProperty(OutputKeys.INDENT, (indentation != 0) ? YES : NO); t.setOutputProperty(OutputKeys.METHOD, "xml"); //xml, html, text t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indentation); return t; }
From source file:Main.java
/** * //from w w w . ja v a2s .c o m * <B>Purpose:</B> XML transformation using XSL * * @param doc * @param xslInput * @param systemid * @return * @throws TransformerException */ public static Node transform(Document doc, StreamSource xslInput, String systemid) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xslInput); DOMResult domResult = new DOMResult(); DOMSource xmlDomSource = null; xmlDomSource = new DOMSource(doc); xmlDomSource.setSystemId(systemid); 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.transform(xmlDomSource, domResult); return domResult.getNode(); }
From source file:com.cisco.dvbu.ps.common.adapters.util.XmlUtils.java
public static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); try {/*from www . j a va2s . com*/ 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 te) { log.error("Xml to String Transformation error!" + te.getMessage()); throw te; } return sw.toString(); }
From source file:Main.java
public static void writeDocument(Document doc, OutputStream os) throws IOException { try {//from w ww . j a v a 2s . co m TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); DOMSource source = new DOMSource(doc); CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder(); StreamResult result = new StreamResult(new OutputStreamWriter(os, utf8Encoder)); transformer.transform(source, result); } catch (TransformerException e) { System.err.println(e.getMessage()); } }
From source file:com.syrup.storage.xml.XmlFactory.java
/** * Convert document to string. Helper method. * //ww w. j a v a 2 s. c om * @param document * the document object. * @return String. * @throws java.io.IOException when unable to write the xml * @throws javax.xml.transform.TransformerException when unable to transform the document */ public static String documentToString(Document document) throws IOException, TransformerException { String soapRequest = null; if (document != null) { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, HTTP.UTF_8); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(document); transformer.transform(source, result); return result.getWriter().toString(); } return soapRequest; }
From source file:Main.java
public static String getXmlString(Node xmlNode) throws TransformerFactoryConfigurationError, TransformerException { 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(xmlNode); transformer.transform(source, result); return result.getWriter().toString(); }