List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:dk.statsbiblioteket.util.xml.DOM.java
/** * Convert the given DOM to an UTF-8 XML String. * * @param dom the Document to convert. * @param withXmlDeclaration if trye, an XML-declaration is prepended. * @return the dom as an XML String./*from www. j av a 2 s .co m*/ * @throws TransformerException if the dom could not be converted. */ // TODO: Consider optimizing this with ThreadLocal Transformers public static String domToString(Node dom, boolean withXmlDeclaration) throws TransformerException { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); if (withXmlDeclaration) { t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } else { t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } t.setOutputProperty(OutputKeys.METHOD, "xml"); /* Transformer */ StringWriter sw = new StringWriter(); t.transform(new DOMSource(dom), new StreamResult(sw)); return sw.toString(); }
From source file:org.biopax.validator.api.ValidatorUtils.java
/** * Writes the multiple results report./*w ww . ja v a 2s. c o m*/ * (as transformed XML). * * @param validatorResponse * @param writer */ public static void write(ValidatorResponse validatorResponse, Writer writer, Source xslt) { try { if (xslt != null) { Document doc = asDocument(validatorResponse); Source xmlSource = new DOMSource(doc); Result result = new StreamResult(writer); TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(xslt); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); trans.transform(xmlSource, result); } else { // write without any xslt getMarshaller().marshal(validatorResponse, writer); } } catch (Exception e) { throw new RuntimeException("Cannot transform/serialize/write: " + validatorResponse, e); } }
From source file:Main.java
private static void SaveCustomerFile(Document CustomerDoc, JFrame mainFrame) { try {/*from w w w.ja v a 2 s . c o m*/ TransformerFactory Factory = TransformerFactory.newInstance(); Transformer Trans = Factory.newTransformer(); DOMSource source = new DOMSource(CustomerDoc); File f = new File("."); String FilePath = f.getAbsoluteFile().getParent() + "\\Customers.xml"; f = new File(FilePath); if (f.exists()) { f.delete(); } StreamResult Result = new StreamResult(f); Trans.setOutputProperty(OutputKeys.INDENT, "yes"); Trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "7"); Trans.transform(source, Result); } catch (TransformerException ex) { System.out.println(ex.getMessage()); JOptionPane.showMessageDialog(mainFrame, "There Was an Error Saving The File. Please Restart the Application."); System.exit(1); } }
From source file:com.vmware.identity.samlservice.Shared.java
/** * Print out XML node to a stream/*from www .j a v a 2 s .com*/ * * @param xml * @param out * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException * @throws UnsupportedEncodingException */ private static final void prettyPrint(Node xml, OutputStream out) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); // tFactory.setAttribute("indent-number", 4); Transformer tf = tFactory.newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty(OutputKeys.METHOD, "xml"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); StreamResult result = new StreamResult(new OutputStreamWriter(out, "UTF-8")); tf.transform(new DOMSource(xml), result); }
From source file:gov.nih.nci.cabig.caaers.utils.pdf.MedwatchUtils.java
public static String evalXPathsOnNode(Node n, String strXPath) { try {//ww w. j a va 2 s . c o m TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource domSource = new DOMSource(n); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(domSource, result); return evalXPathOnXML(sw.toString(), strXPath); } catch (Exception e) { log.debug(e); } return ""; }
From source file:com.mobilefirst.fiberlink.WebServiceRequest.java
/** * prettyFormatXML translates the input xml and creates the indentation you would like to achieve * @param input: XML String// w ww. j av a2 s . co m * @param indent: Integer for how much indent to specify */ public static String prettyFormatXML(String input, Integer indent) { try { 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); javax.xml.transform.Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent.toString()); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.aurel.track.exchange.docx.exporter.CustomXML.java
/** * @param items/* w ww. j a va 2 s .c om*/ * @return */ public static void convertToXml(OutputStream outputStream, Document dom) { Transformer transformer = null; try { TransformerFactory factory = TransformerFactory.newInstance(); transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (TransformerConfigurationException e) { LOGGER.error( "Creating the transformer failed with TransformerConfigurationException: " + e.getMessage()); return; } try { transformer.transform(new DOMSource(dom), new StreamResult(outputStream)); } catch (TransformerException e) { LOGGER.error("Transform failed with TransformerException: " + e.getMessage()); } }
From source file:io.cloudslang.content.xml.utils.XmlUtils.java
private static String transformElementNode(Node node) throws TransformerException { StringWriter stringWriter = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, Constants.YES); transformer.setOutputProperty(OutputKeys.INDENT, Constants.YES); transformer.transform(new DOMSource(node), new StreamResult(stringWriter)); return stringWriter.toString().trim(); }
From source file:com.wavemaker.tools.pws.install.PwsInstall.java
public static void insertImport(File xmlFile, String resource) throws Exception { String content = getTrimmedXML(xmlFile); String fromStr1 = "<!DOCTYPE"; String toStr1 = "<!--!DOCTYPE"; content = content.replace(fromStr1, toStr1); String fromStr2 = "spring-beans-2.0.dtd\">"; String toStr2 = "spring-beans-2.0.dtd\"-->"; content = content.replace(fromStr2, toStr2); FileUtils.writeStringToFile(xmlFile, content, "UTF-8"); InputStream is = new FileInputStream(xmlFile); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w w w . j av a 2s .com*/ DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.parse(is); doc = insertImport(doc, resource); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); t.transform(new DOMSource(doc), new StreamResult(xmlFile)); content = FileUtils.readFileToString(xmlFile, "UTF-8"); content = content.replace(toStr1, fromStr1); content = content.replace(toStr2, fromStr2); FileUtils.writeStringToFile(xmlFile, content, "UTF-8"); }
From source file:com.wavemaker.tools.pws.install.PwsInstall.java
public static void insertEntryKey(File xmlFile, File[] runtimeJarFiles, File[] toolsJarFiles, String partnerName) throws Exception { String content = getTrimmedXML(xmlFile); String fromStr1 = "<!DOCTYPE"; String toStr1 = "<!--!DOCTYPE"; content = content.replace(fromStr1, toStr1); String fromStr2 = "spring-beans-2.0.dtd\">"; String toStr2 = "spring-beans-2.0.dtd\"-->"; content = content.replace(fromStr2, toStr2); FileUtils.writeStringToFile(xmlFile, content, "UTF-8"); InputStream is = new FileInputStream(xmlFile); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//w w w. j a v a2s . c o m DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.parse(is); insertEntryKey(doc, runtimeJarFiles, toolsJarFiles, partnerName); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); t.transform(new DOMSource(doc), new StreamResult(xmlFile)); content = FileUtils.readFileToString(xmlFile, "UTF-8"); content = content.replace(toStr1, fromStr1); content = content.replace(toStr2, fromStr2); FileUtils.writeStringToFile(xmlFile, content, "UTF-8"); }