List of usage examples for javax.xml.transform OutputKeys OMIT_XML_DECLARATION
String OMIT_XML_DECLARATION
To view the source code for javax.xml.transform OutputKeys OMIT_XML_DECLARATION.
Click Source Link
From source file:Main.java
public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory .newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static String formatXMLStr(String xml) { String output = null;// w ww .ja va 2 s. c o m try { Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); // output = writer.getBuffer().toString().replaceAll("\n|\r", ""); output = writer.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return output; }
From source file:Main.java
public static void nodeToString(Node node, StringBuffer buf) throws TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource dSource = new DOMSource(node); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(dSource, sr);//from ww w. j a va2 s . c o m StringWriter anotherSW = (StringWriter) sr.getWriter(); buf.append(anotherSW.getBuffer()); }
From source file:Main.java
/** * Return the contents of the node as a string. Any exceptions are ignored and the method returns null. * // w ww . j a v a 2 s. c om * @param node * @param indent * Indent the XML when formatting * @return The node contents */ public static String getString(Node node, boolean indent) { Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, (indent) ? "yes" : "no"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(node); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerConfigurationException e) { //e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { //e.printStackTrace(); } catch (TransformerException e) { //e.printStackTrace(); } return ""; }
From source file:Main.java
/** * Serialize an XML Element into a String. * @param e Element to be serialized.//from w ww . j av a 2 s . c om * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration. * @return String representation of the XML document fragment. */ public static final String serialize(Element e, boolean omitXMLDeclaration) { if (e != null) { try { DOMSource domSource = new DOMSource(e); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ((omitXMLDeclaration) ? "yes" : "no")); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.transform(domSource, new StreamResult(baos)); baos.close(); return new String(baos.toByteArray(), UTF8_ENCODING); } catch (Throwable t) { } } return null; }
From source file:Main.java
public static String nodeToString(final Node node, final boolean omitXMLDecl) { final StringWriter writer = new StringWriter(); final Transformer transformer; try {//w w w. jav a 2s.c o m transformer = TransformerFactory.newInstance().newTransformer(); } catch (final TransformerException e) { throw new AssertionError("No errors expected when creating an identity transformer", e); } if (omitXMLDecl) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } try { transformer.transform(new DOMSource(node), new StreamResult(writer)); } catch (final TransformerException e) { throw new AssertionError("No errors expected during identity transformation", e); } return writer.toString(); }
From source file:Main.java
public static String createString(Element element) { try {/*from w ww . java 2s . co m*/ TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource source = new DOMSource(element); Writer writer = new StringWriter(); Result result = new StreamResult(writer); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); return writer.toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
/** * returns an XML string.// w ww . j a va 2 s. c o m * * @param pDocument Document XML DOM document * @return String XML string */ public static String getXML(Document pDocument) throws Exception { String retString = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.transform(new DOMSource(pDocument), new StreamResult(out)); retString = out.toString(); out.close(); } catch (Exception ex) { throw new Exception(ex.getMessage()); } return retString; }
From source file:Main.java
public final static void applyXSLTTransform(final Reader xslt, final Reader input, final Writer output) throws TransformerException { final Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt)); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OUTPUT_PROPERTY_YES); transformer.setOutputProperty(OutputKeys.INDENT, OUTPUT_PROPERTY_NO); transformer.transform(new StreamSource(input), new StreamResult(output)); }
From source file:Main.java
/** * Uses a TransformerFactory with an identity transformation to convert a * Document into a String representation of the XML. * * @param document Document.//from w w w . jav a 2s . c o m * @return An XML String. * @throws IOException if an error occurs during transformation. */ public static String documentToString(Document document) throws IOException { String xml = null; try { DOMSource dom = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult output = new StreamResult(writer); // Use Transformer to serialize a DOM TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // No need for pretty printing transformer.setOutputProperty(OutputKeys.INDENT, INDENT_XML); // XML Declarations unexpected whitespace for legacy AS XMLDocument type, // so we always omit it. We can't tell whether one was present when // constructing the Document in the first place anyway... transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION); transformer.transform(dom, output); xml = writer.toString(); } catch (TransformerException te) { throw new IOException("Error serializing Document as String: " + te.getMessageAndLocation()); } return xml; }