List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:Main.java
public static void serializeDocument(Document doc, OutputStream outputStream) throws Exception { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer = tfactory.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); serializer.transform(new DOMSource(doc), new StreamResult(outputStream)); }
From source file:Main.java
/** * Serialized a node to a string. XML declarations will be omitted. * /*w w w. j av a2 s .c o m*/ * @param node The node to be serialized. * * @return The serialized node. * * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:uk.ac.diamond.shibbolethecpauthclient.Utils.java
/** * Helper method that turns the message into a formatted XML string. * /*from w w w. j av a2s.c om*/ * @param aObject *The XML object to turn into a formatted XML string * * @return The XML string * * @throws IOException * thrown if there is a problem turning the XML object into a string */ static String xmlToString(XMLObject aObject) throws IOException { Document doc; try { doc = Configuration.getMarshallerFactory().getMarshaller(aObject).marshall(aObject).getOwnerDocument(); } catch (MarshallingException e) { throw new IOException(e); } try { Source source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerException e) { throw new IOException(e); } }
From source file:Main.java
public static String getXMLString(Node doc) throws TransformerException, TransformerConfigurationException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); StringWriter sw = new StringWriter(); Source src = new DOMSource(doc); Result dest = new StreamResult(sw); aTransformer.transform(src, dest); return sw.toString(); }
From source file:Main.java
/** * Writes an xml representation to a stream. * * @param doc/*from w ww .ja v a 2 s . c o m*/ * the document * @param out * the output stream * @throws IOException * if there is an error transforming the dom to a stream */ public static void toXml(Document doc, OutputStream out) throws IOException { try { DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.VERSION, doc.getXmlVersion()); transformer.transform(domSource, result); } catch (TransformerException e) { throw new IOException("unable to transform dom to a stream"); } }
From source file:Main.java
public static String xmlDocToString(Document document) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); return writer.getBuffer().toString(); }
From source file:Main.java
/** export DOM document to a file -- handy for debugging **/ public static void saveDocAsXML(Document doc, String filename) { try {/*www. jav a 2 s.c om*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); Result output = new StreamResult(new File(filename)); Source input = new DOMSource(doc); transformer.transform(input, output); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.sonar.maven.it.ItUtils.java
/** * Creates a settings xml with a sonar profile, containing all the given properties * Also adds repox to continue to use QAed artifacts */// www . ja v a2 s . c o m public static String createSettingsXml(Map<String, String> props) throws Exception { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element settings = doc.createElement("settings"); Element profiles = doc.createElement("profiles"); Element profile = doc.createElement("profile"); Element id = doc.createElement("id"); id.setTextContent("sonar"); Element properties = doc.createElement("properties"); for (Map.Entry<String, String> e : props.entrySet()) { Element el = doc.createElement(e.getKey()); el.setTextContent(e.getValue()); properties.appendChild(el); } profile.appendChild(id); profile.appendChild(properties); profile.appendChild(createRepositories(doc)); profile.appendChild(createPluginRepositories(doc)); profiles.appendChild(profile); settings.appendChild(profiles); doc.appendChild(settings); Writer writer = new StringWriter(); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static void writeXml(Document doc, File outputFile) { try {/*w w w . j a v a 2 s .co m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); Result output = new StreamResult(outputFile); Source input = new DOMSource(doc); transformer.transform(input, output); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:Main.java
/** * Performs XSL transformation.//from w w w . j a v a 2 s.c o m * @param xmlDoc Document * @param xslDoc Document * @throws TransformerConfigurationException * @throws TransformerException * @return String The output text. */ public static String xslTransform(Document xmlDoc, Document xslDoc) throws TransformerConfigurationException, TransformerException { DOMSource source = new DOMSource(xmlDoc); DOMSource xslSrc = new DOMSource(xslDoc); TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer transformer = xformFactory.newTransformer(xslSrc); java.io.StringWriter sw = new java.io.StringWriter(); StreamResult outXml = new StreamResult(sw); transformer.transform(source, outXml); return sw.toString(); }