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
/** * Save the XML document to a file./*from w w w .j av a 2 s . co m*/ * * @param doc * The XML document to save. * @param file * The file to save the document to. * @param encoding * The encoding to save the file as. * * @throws TransformerException * If there is an error while saving the XML. */ public static void save(Document doc, String file, String encoding) throws TransformerException { try { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file final Result result = new StreamResult(new File(file)); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
private static String nodeToString(Node node) throws Exception { StringWriter sw = new StringWriter(); Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static byte[] doc2bytes(Document doc, boolean formated) { try {/*from www . j ava 2 s . c o m*/ Source source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (formated) { // linefeed formatting transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } else { // remove xml header transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); return out.toByteArray(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out). * * @param _docOrNode {@link Document} or {@link Node} object * @param _outStream {@link OutputStream} to print on * @throws IOException on error//from w w w . ja va 2 s. c o m */ public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException { if (_docOrNode == null || _outStream == null) { throw new IOException("Cannot print (on) 'null' object"); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { 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(_docOrNode), new StreamResult(new OutputStreamWriter(_outStream, "UTF-8"))); } catch (UnsupportedEncodingException | TransformerException _ex) { throw new IOException("Could not print Document or Node.", _ex); } }
From source file:Main.java
public static String formatXML(String unformatted) throws SAXException, IOException, TransformerException, ParserConfigurationException { if (unformatted == null) return null; // remove whitespaces between xml tags String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><"); // Instantiate transformer input Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces)); StreamResult xmlOutput = new StreamResult(new StringWriter()); // Configure transformer Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); String formatted = xmlOutput.getWriter().toString(); return formatted; }
From source file:Main.java
public static void saveDocument(final Document document, String filename) throws TransformerException { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); t.transform(source, result);/*from w ww . jav a 2s . c o m*/ }
From source file:Main.java
/** * Creates a string representation of a {@link Node} instance. This method * does not introduce any character to the string representation of the * {@link Node} (eg. \n or \r characters) * * @param node A {@link Node} instance/*from w ww. j a va 2 s .c om*/ * @return A string representation of the node instance * @throws RequestSecurityTokenException */ public static String xmlToString(Node node) throws Exception { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } catch (TransformerException e) { e.printStackTrace(); throw new Exception("Cannot build a string representation of the assertion."); } }
From source file:Main.java
public static void format(Source source, boolean omitDeclaration, Result result) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); try {//from w w w. j a v a2 s.co m transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (IllegalArgumentException exc) { // not supported } if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); }
From source file:net.sf.joost.Main.java
/** * Entry point/*from w w w.j av a 2s .c o m*/ * @param args array of strings containing the parameter for Joost and * at least two URLs addressing xml-source and stx-sheet */ public static void main(String[] args) { // input filename String xmlFile = null; // the currently last processor (as XMLFilter) Processor processor = null; // output filename (optional) String outFile = null; // custom message emitter class name (optional) String meClassname = null; // log4j properties filename (optional) String log4jProperties = null; // log4j message level (this is an object of the class Level) Level log4jLevel = null; // set to true if a command line parameter was wrong boolean wrongParameter = false; // set to true if -help was specified on the command line boolean printHelp = false; // set to true if -pdf was specified on the command line boolean doFOP = false; // set to true if -nodecl was specified on the command line boolean nodecl = false; // set to true if -noext was specified on the command line boolean noext = false; // set to true if -doe was specified on the command line boolean doe = false; // debugging boolean dontexit = false; // timings boolean measureTime = false; long timeStart = 0, timeEnd = 0; // needed for evaluating parameter assignments int index; // serializer SAX -> XML text StreamEmitter emitter = null; // filenames for the usage and version info final String USAGE = "usage.txt", VERSION = "version.txt"; try { // parse command line argument list for (int i = 0; i < args.length; i++) { if (args[i].trim().length() == 0) { // empty parameter? ingore } // all options start with a '-', but a single '-' means stdin else if (args[i].charAt(0) == '-' && args[i].length() > 1) { if ("-help".equals(args[i])) { printHelp = true; continue; } else if ("-version".equals(args[i])) { printResource(VERSION); logInfoAndExit(); } else if ("-pdf".equals(args[i])) { doFOP = true; continue; } else if ("-nodecl".equals(args[i])) { nodecl = true; continue; } else if ("-noext".equals(args[i])) { noext = true; continue; } else if ("-doe".equals(args[i])) { doe = true; continue; } else if ("-wait".equals(args[i])) { dontexit = true; // undocumented continue; } else if ("-time".equals(args[i])) { measureTime = true; continue; } else if ("-o".equals(args[i])) { // this option needs a parameter if (++i < args.length && args[i].charAt(0) != '-') { if (outFile != null) { System.err.println("Option -o already specified with " + outFile); wrongParameter = true; } else outFile = args[i]; continue; } else { if (outFile != null) System.err.println("Option -o already specified with " + outFile); else System.err.println("Option -o requires a filename"); i--; wrongParameter = true; } } else if ("-m".equals(args[i])) { // this option needs a parameter if (++i < args.length && args[i].charAt(0) != '-') { if (meClassname != null) { System.err.println("Option -m already specified with " + meClassname); wrongParameter = true; } else meClassname = args[i]; continue; } else { if (meClassname != null) System.err.println("Option -m already specified with " + meClassname); else System.err.println("Option -m requires a classname"); i--; wrongParameter = true; } } else if (DEBUG && "-log-properties".equals(args[i])) { // this option needs a parameter if (++i < args.length && args[i].charAt(0) != '-') { log4jProperties = args[i]; continue; } else { System.err.println("Option -log-properties requires " + "a filename"); wrongParameter = true; } } else if (DEBUG && "-log-level".equals(args[i])) { // this option needs a parameter if (++i < args.length && args[i].charAt(0) != '-') { if ("off".equals(args[i])) { log4jLevel = Level.OFF; continue; } else if ("debug".equals(args[i])) { log4jLevel = Level.DEBUG; continue; } else if ("info".equals(args[i])) { log4jLevel = Level.INFO; continue; } else if ("warn".equals(args[i])) { log4jLevel = Level.WARN; continue; } else if ("error".equals(args[i])) { log4jLevel = Level.ERROR; continue; } else if ("fatal".equals(args[i])) { log4jLevel = Level.FATAL; continue; } else if ("all".equals(args[i])) { log4jLevel = Level.ALL; continue; } else { System.err.println("Unknown parameter for -log-level: " + args[i]); wrongParameter = true; continue; } } else { System.err.println("Option -log-level requires a " + "parameter"); wrongParameter = true; } } else { System.err.println("Unknown option " + args[i]); wrongParameter = true; } } // command line argument is not an option with a leading '-' else if ((index = args[i].indexOf('=')) != -1) { // parameter assignment if (processor != null) processor.setParameter(args[i].substring(0, index), args[i].substring(index + 1)); else { System.err.println("Assignment " + args[i] + " must follow an stx-sheet parameter"); wrongParameter = true; } continue; } else if (xmlFile == null) { xmlFile = args[i]; continue; } else { // xmlFile != null, i.e. this is an STX sheet ParseContext pContext = new ParseContext(); pContext.allowExternalFunctions = !noext; if (measureTime) timeStart = System.currentTimeMillis(); Processor proc = new Processor(new InputSource(args[i]), pContext); if (nodecl) proc.outputProperties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); if (measureTime) { timeEnd = System.currentTimeMillis(); System.err.println("Parsing " + args[i] + ": " + (timeEnd - timeStart) + " ms"); } if (processor != null) proc.setParent(processor); // XMLFilter chain processor = proc; } } // PDF creation requested if (doFOP && outFile == null) { System.err.println("Option -pdf requires option -o"); wrongParameter = true; } // missing filenames if (!printHelp && processor == null) { if (xmlFile == null) System.err.println("Missing filenames for XML source and " + "STX transformation sheet"); else System.err.println("Missing filename for STX transformation " + "sheet"); wrongParameter = true; } if (meClassname != null && !wrongParameter) { // create object StxEmitter messageEmitter = null; try { messageEmitter = (StxEmitter) Class.forName(meClassname).newInstance(); } catch (ClassNotFoundException ex) { System.err.println("Class not found: " + ex.getMessage()); wrongParameter = true; } catch (InstantiationException ex) { System.err.println("Instantiation failed: " + ex.getMessage()); wrongParameter = true; } catch (IllegalAccessException ex) { System.err.println("Illegal access: " + ex.getMessage()); wrongParameter = true; } catch (ClassCastException ex) { System.err.println( "Wrong message emitter: " + meClassname + " doesn't implement the " + StxEmitter.class); wrongParameter = true; } if (messageEmitter != null) { // i.e. no exception occurred // set message emitter for all processors in the filter chain Processor p = processor; do { p.setMessageEmitter(messageEmitter); Object o = p.getParent(); if (o instanceof Processor) p = (Processor) o; else p = null; } while (p != null); } } if (printHelp) { printResource(VERSION); printResource(USAGE); logInfoAndExit(); } if (wrongParameter) { System.err.println("Specify -help to get a detailed help message"); System.exit(1); } if (DEBUG) { // use specified log4j properties file if (log4jProperties != null) PropertyConfigurator.configure(log4jProperties); // set log level specified on the the command line if (log4jLevel != null) Logger.getRootLogger().setLevel(log4jLevel); } // The first processor re-uses its XMLReader for parsing the input // xmlFile. // For a real XMLFilter usage you have to call // processor.setParent(yourXMLReader) // Connect a SAX consumer if (doFOP) { // pass output events to FOP // // Version 1: use a FOPEmitter object as XMLFilter // processor.setContentHandler( // new FOPEmitter( // new java.io.FileOutputStream(outFile))); // Version 2: use a static method to retrieve FOP's content // handler and use it directly processor.setContentHandler(FOPEmitter.getFOPContentHandler(new java.io.FileOutputStream(outFile))); } else { // Create XML output if (outFile != null) { emitter = StreamEmitter.newEmitter(outFile, processor.outputProperties); emitter.setSystemId(new File(outFile).toURI().toString()); } else emitter = StreamEmitter.newEmitter(System.out, processor.outputProperties); processor.setContentHandler(emitter); processor.setLexicalHandler(emitter); // the previous line is a short-cut for // processor.setProperty( // "http://xml.org/sax/properties/lexical-handler", emitter); emitter.setSupportDisableOutputEscaping(doe); } InputSource is; if (xmlFile.equals("-")) { is = new InputSource(System.in); is.setSystemId("<stdin>"); is.setPublicId(""); } else is = new InputSource(xmlFile); // Ready for take-off if (measureTime) timeStart = System.currentTimeMillis(); processor.parse(is); if (measureTime) { timeEnd = System.currentTimeMillis(); System.err.println("Processing " + xmlFile + ": " + (timeEnd - timeStart) + " ms"); } // // check if the Processor copy constructor works // Processor pr = new Processor(processor); // java.util.Properties props = new java.util.Properties(); // props.put("encoding", "ISO-8859-2"); // StreamEmitter em = // StreamEmitter.newEmitter(System.err, props); // pr.setContentHandler(em); // pr.setLexicalHandler(em); // pr.parse(is); // // end check // this is for debugging with the Java Memory Profiler if (dontexit) { System.err.println("Press Enter to exit"); System.in.read(); } } catch (java.io.IOException ex) { System.err.println(ex.toString()); System.exit(1); } catch (SAXException ex) { if (emitter != null) { try { // flushes the internal BufferedWriter, i.e. outputs // the intermediate result emitter.endDocument(); } catch (SAXException exx) { // ignore } } Exception embedded = ex.getException(); if (embedded != null) { if (embedded instanceof TransformerException) { TransformerException te = (TransformerException) embedded; SourceLocator sl = te.getLocator(); String systemId; // ensure that systemId is not null; is this a bug? if (sl != null && (systemId = sl.getSystemId()) != null) { // remove the "file://" scheme prefix if it is present if (systemId.startsWith("file://")) systemId = systemId.substring(7); else if (systemId.startsWith("file:")) // bug in JDK 1.4 / Crimson? (see rfc1738) systemId = systemId.substring(5); System.err.println(systemId + ":" + sl.getLineNumber() + ":" + sl.getColumnNumber() + ": " + te.getMessage()); } else System.err.println(te.getMessage()); } else { // Fatal: this mustn't happen embedded.printStackTrace(System.err); } } else System.err.println(ex.toString()); System.exit(1); } }
From source file:Main.java
public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); 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", ""); }