List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:net.bpelunit.framework.control.util.BPELUnitUtil.java
private static String serializeXML(Node node) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); ByteArrayOutputStream bOS = new ByteArrayOutputStream(); t.transform(new DOMSource(node), new StreamResult(bOS)); return bOS.toString(); }
From source file:Main.java
public static void writeXMLObject(String path, String name, String tag, String model, double ry, String version, double x, double y, double z) { try {// www. j a va 2s . c o m id++; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // Root elements Document doc = docBuilder.newDocument(); doc.setXmlStandalone(true); Element rootElement = doc.createElement(tag); doc.appendChild(rootElement); rootElement.setAttributeNode(addAttribut(doc, "action", "void")); rootElement.setAttributeNode(addAttribut(doc, "actionDist", "10.0")); rootElement.setAttributeNode(addAttribut(doc, "collidable", "true")); rootElement.setAttributeNode(addAttribut(doc, "id", id + "")); rootElement.setAttributeNode(addAttribut(doc, "model", model)); rootElement.setAttributeNode(addAttribut(doc, "pCameraFacing", "true")); rootElement.setAttributeNode(addAttribut(doc, "pControlFlow", "false")); rootElement.setAttributeNode(addAttribut(doc, "pEmitH", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitInnerRadius", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitOutterRadius", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitType", "0")); rootElement.setAttributeNode(addAttribut(doc, "pEmitW", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndA", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndB", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndMass", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndR", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndSize", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pEndV", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pFactoryNumber", "500")); rootElement.setAttributeNode(addAttribut(doc, "pInitialVelocity", "0.0030")); rootElement.setAttributeNode(addAttribut(doc, "pMaxAngle", "10.0")); rootElement.setAttributeNode(addAttribut(doc, "pMaxLife", "2000.0")); rootElement.setAttributeNode(addAttribut(doc, "pMinAngle", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "pMinLife", "1000.0")); rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSecVar", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSeconds", "100")); rootElement.setAttributeNode(addAttribut(doc, "pSpeed", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartA", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartB", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartMass", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartR", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartSize", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "pStartV", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "rx", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "ry", ry + "")); rootElement.setAttributeNode(addAttribut(doc, "rz", "0.0")); rootElement.setAttributeNode(addAttribut(doc, "s", "1.0")); rootElement.setAttributeNode(addAttribut(doc, "type", "basic")); rootElement.setAttributeNode(addAttribut(doc, "versionCode", version)); rootElement.setAttributeNode(addAttribut(doc, "x", x + "")); rootElement.setAttributeNode(addAttribut(doc, "y", y + "")); rootElement.setAttributeNode(addAttribut(doc, "z", z + "")); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(path + "\\" + name + id + ".xml"); transformer.transform(source, result); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } }
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 a v a 2s. co m * @param doc *The XML document to turn into a formatted XML string * * @return The XML string */ static String xmlToString(Element doc) { StringWriter sw = new StringWriter(); try { 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(sw)); return sw.toString(); } catch (TransformerException e) { LOG.error("Unable to print message contents: ", e); return "<ERROR: " + e.getMessage() + ">"; } }
From source file:com.c4om.utils.xmlutils.JavaXMLUtils.java
/** * Method that converts a W3C {@link Document} object to a String * @param document the document to convert * @param indent whether lines must be indented or not * @param omitDeclaration whether the XML declaration should be omitted or not. * @param encoding the encoding placed at the XML declaration. Be carefully: Specifying here an encoding only takes effect at the XML declaration. If you are going to write the string to an output stream (e.g. to a file), you must also take care of the encoding there, for example, by means of {@link org.apache.commons.io.output.XmlStreamWriter}. * @return the {@link String} representation of the document * @throws TransformerException if there are problems during the conversion */// w w w.j av a 2 s . c om public static String convertW3CDocumentToString(Document document, boolean indent, boolean omitDeclaration, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
private static void writeTo(Result result, Document document) throws TransformerConfigurationException, TransformerException, FileNotFoundException, UnsupportedEncodingException { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(4)); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N // indent the output to make it more legible... try {/*from w w w .ja va 2s . c o m*/ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N } catch (IllegalArgumentException e) { // the JAXP implementation doesn't support indentation, no big deal //e.printStackTrace(); } transformer.transform(source, result); }
From source file:com.opendoorlogistics.core.utils.XMLUtils.java
public static String toString(Node node) { try {/*from ww w .j a v a2s.com*/ TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(node); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); return writer.toString(); } catch (Throwable e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Converts a dom to a String//from www . jav a 2 s . c o m * * @param dom * dom to convert * @param outputProperties * the properties for the String representation of the XML * @return the dom as a String */ public static String writeDomToString(Document dom, Properties outputProperties) { try { StringWriter ret = new StringWriter(); TransformerFactory transFact = TransformerFactory.newInstance(); // transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(); if (outputProperties != null) transformer.setOutputProperties(outputProperties); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(ret); transformer.transform(source, result); return ret.toString(); } catch (Exception e) { throw new RuntimeException("Could not write dom to string!", e); } }
From source file:uk.ac.diamond.shibbolethecpauthclient.Utils.java
/** * Helper method that turns the message into a formatted XML string. * //from ww w. j a v a 2 s . co m * @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:com.c4om.utils.xmlutils.JavaXMLUtils.java
/** * Method that prints a W3C {@link Document} object to a provided {@link OutputStream}. * Encoding should be correctly specified. * @param document the document to convert * @param os the output stream to print to * @param indent whether lines must be indented or not * @param omitDeclaration whether the XML declaration should be omitted or not. * @param encoding the encoding placed at the XML declaration and used to encode the file. * @return the {@link String} representation of the document * @throws TransformerException if there are problems during the conversion *///ww w. j a v a2 s . c o m public static void printW3CDocumentToOutputStream(Document document, OutputStream os, boolean indent, boolean omitDeclaration, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); XmlStreamWriter writer; if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); writer = new XmlStreamWriter(os, encoding); } else { writer = new XmlStreamWriter(os); } StreamResult streamResult = new StreamResult(writer); transformer.transform(new DOMSource(document), streamResult); }
From source file:Main.java
/** * Convert XML DOM document to a XML string representation * * @param doc// w w w . j a v a2s. co m * XML DOM document * @return XML string * @throws Exception * in error case */ public static String xmlDOMDocumentToString(Document doc) throws Exception { if (doc == null) { throw new RuntimeException("No XML DOM document (null)!"); } StringWriter stringWriter = new StringWriter(); String strDoc = null; try { StreamResult streamResult = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); // transformerFactory.setAttribute("nIndent-number", new Integer(4)); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(new DOMSource(doc.getDocumentElement()), streamResult); stringWriter.flush(); strDoc = stringWriter.toString(); } catch (Exception e) { // Logger.XMLEval.logState("Parsing of XML DOM document failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (stringWriter != null) { stringWriter.close(); stringWriter = null; } } return strDoc; }