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
/** * Create a {@link Transformer} just the way we like it. * * @return Returns a new {@link Transformer}. *///w ww.j a v a 2 s . c o m private static Transformer createTransformer() { final Transformer xform; try { xform = m_xformFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IllegalStateException(e); } xform.setOutputProperty(OutputKeys.INDENT, "yes"); xform.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); return xform; }
From source file:Main.java
/** * Transforms an XML to a String/*from w w w. jav a2 s .com*/ * @param node XML node * @return String represenation of XML */ public static String printXML(Node node) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { serializer = tfactory.newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); StringWriter output = new StringWriter(); serializer.transform(new DOMSource(node), new StreamResult(output)); return output.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String documentToString(Document doc, String xslName) { StringWriter sw = new StringWriter(); try {/*from w ww .j a v a2s . c om*/ TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc.getDocumentElement()); Result result = new StreamResult(sw); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } return head + xslName + sw.toString(); }
From source file:Main.java
/** * convert an XML node to an XML statement * @param node current XML node//from w ww.ja v a 2s . c o m * @return XML string */ public static String nodeToString(Node node) { //MagicBooleans.trace("nodeToString(node: " + node + ")"); StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } String result = sw.toString(); //MagicBooleans.trace("nodeToString() returning: " + result); return result; }
From source file:Main.java
public static void transform(Document src, Writer dst, String xsl) throws TransformerException { DOMSource dsrc = new DOMSource(src); StreamResult res = new StreamResult(dst); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(new StreamSource(xsl)); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(dsrc, res);//from w w w .ja va 2 s .co m }
From source file:Main.java
/** * Convert XML Node to String// w w w. ja v a 2s . c o m * * @param node * the node to convert * @return the String equivalent of the node * @throws TransformerException */ public static String nodeToString(final Node node) throws TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); final Writer out = new StringWriter(); tf.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String XMLToString(Document doc, Boolean singleLine) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {//from w ww . ja va 2s .c o m transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } String string = writer.getBuffer().toString(); if (!singleLine) { return string; } return string.replaceAll("\n|\r", ""); }
From source file:Main.java
public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;//w ww . jav a2 s . c o m try { serializer = tfactory.newTransformer(); if (prettyPrint) { //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } if (!includeXmlDeclaration) { serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } DOMSource xmlSource = new DOMSource(node); StreamResult outputTarget = new StreamResult(out); serializer.transform(xmlSource, outputTarget); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String getXml(Document doc) { DOMSource doms = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); String xml = null;//from w ww . jav a 2 s. co m try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "GB2312"); properties.setProperty(OutputKeys.METHOD, "xml");//! properties.setProperty(OutputKeys.VERSION, "1.0"); properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperties(properties); t.transform(doms, sr); String dtd = doc.getDoctype().getInternalSubset(); if ((null != dtd) && (dtd.length() > 0)) { dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n"; } ; xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd; xml += sw.toString(); } catch (TransformerConfigurationException tce) { //"Transformer Configuration Exception\n-----" } catch (TransformerException te) { //"Transformer Exception\n---------" } return xml; }
From source file:Main.java
public static void print(Node node, OutputStream out) throws UnsupportedEncodingException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 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(node), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }