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:org.apache.streams.gnip.powertrack.ActivityXMLActivitySerializer.java
private String setContentIfEmpty(String xml) throws Exception { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); Document doc = docBuilder.parse(is); doc.getDocumentElement().normalize(); Element base = (Element) doc.getElementsByTagName("entry").item(0); NodeList nodeList = base.getChildNodes(); // for(int i=0; i < nodeList.getLength(); ++i) { // System.out.println(nodeList.item(i).getNodeName()); // }/*ww w . j av a2 s .c o m*/ Element obj = (Element) base.getElementsByTagName("activity:object").item(0); Element content = (Element) obj.getElementsByTagName("content").item(0); // System.out.println("Number of child nodes : "+content.getChildNodes().getLength()); // System.out.println("Text content before : "+content.getTextContent()); if (content.getTextContent() == null || content.getTextContent().equals("")) { content.setTextContent(" "); } // System.out.println("Number of child nodes after : "+content.getChildNodes().getLength()); // System.out.println("Text content after : "+content.getTextContent()); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); // System.out.println(output); // System.out.println(output); // System.out.println(content); return output; }
From source file:com.moviejukebox.tools.DOMHelper.java
/** * Write the Document out to a file using nice formatting * * @param doc The document to save/* www.j a va2 s .c o m*/ * @param localFile The file to write to * @return */ public static boolean writeDocumentToFile(Document doc, File localFile) { try { Transformer trans = TransformerFactory.newInstance().newTransformer(); // Define the output properties trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); trans.setOutputProperty(OutputKeys.INDENT, YES); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); doc.setXmlStandalone(true); trans.transform(new DOMSource(doc), new StreamResult(localFile)); return true; } catch (IllegalArgumentException | DOMException | TransformerException error) { LOG.error("Error writing the document to {}", localFile); LOG.error("Message: {}", error.getMessage()); return false; } }
From source file:curam.molsa.test.customfunctions.MOLSADOMReader.java
/** * Transforms an inbound DOM document according to a specific stylesheet. * //from w ww .j a v a 2 s . c o m * @param document * Contains the Inbound DOM document. * @param stylesheet * The file location of the stylesheet use to transform the document * @param rootElement * The expected root element of the transformed document * @return The transformed document */ public Document transformDocumentBasedOnXSL(final Document document, final String stylesheet, final String responseRootElement) throws ParserConfigurationException, SAXException, IOException { String xmlString = ""; Document transformedDocument = null; final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); dbfac.setNamespaceAware(true); final DocumentBuilder builder = dbfac.newDocumentBuilder(); try { final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(stylesheet); final Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(inputStream)); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); final StringWriter stringWriter = new StringWriter(); final StreamResult streamResult = new StreamResult(stringWriter); final DOMSource source = new DOMSource(document); transformer.transform(source, streamResult); xmlString = stringWriter.toString(); if (xmlString.contains(responseRootElement)) { transformedDocument = builder.parse(new InputSource(new StringReader(xmlString))); } else { transformedDocument = builder.newDocument(); } } catch (final TransformerConfigurationException tcExp) { tcExp.printStackTrace(); } catch (final TransformerException tExp) { tExp.printStackTrace(); } return transformedDocument; }
From source file:mx.bigdata.sat.cfdi.TFDv1_v32.java
private String nodeToString(Element node) { StringWriter sw = new StringWriter(); try {/*from w w w . j a v a2s . co m*/ Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString(); }
From source file:com.twinsoft.convertigo.engine.util.XMLUtils.java
public static void prettyPrintDOMWithEncoding(Document doc, String defaultEncoding, Result result) { Node firstChild = doc.getFirstChild(); boolean omitXMLDeclaration = false; String encoding = defaultEncoding; // default Encoding char set if non // is found in the PI if ((firstChild.getNodeType() == Document.PROCESSING_INSTRUCTION_NODE) && (firstChild.getNodeName().equals("xml"))) { omitXMLDeclaration = true;/*w ww . j a v a2 s . c o m*/ String piValue = firstChild.getNodeValue(); // extract from PI the encoding Char Set int encodingOffset = piValue.indexOf("encoding=\""); if (encodingOffset != -1) { encoding = piValue.substring(encodingOffset + 10); // remove the last " encoding = encoding.substring(0, encoding.length() - 1); } } try { Transformer t = getNewTransformer(); t.setOutputProperty(OutputKeys.ENCODING, encoding); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, text t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXMLDeclaration ? "yes" : "no"); t.transform(new DOMSource(doc), result); } catch (Exception e) { Engine.logEngine.error("Unexpected exception while pretty print DOM", e); } }
From source file:de.awtools.xml.XSLTransformer.java
/** * Liefert den Transformer.//from www . j a v a 2 s. c om * * @return Ein <code>Transformer</code> */ private final Transformer getTransformer() { if ((transe == null) || (styleModified)) { InputStream xslt = null; try { xslt = getStyle(); transe = TransformerUtils.getTransformer(xslt, getUriResolver()); transe.setOutputProperty(OutputKeys.ENCODING, getEncoding()); transe.setOutputProperty(OutputKeys.METHOD, getMethod()); transe.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, getOmitXmlDeclaration()); styleModified = false; } catch (IOException ex) { log.debug("IOException", ex); throw new RuntimeException(ex); } finally { IOUtils.closeQuietly(xslt); } } return transe; }
From source file:com.krawler.formbuilder.servlet.workflowHandler.java
public void exportWorkFLow(HttpServletRequest request, HttpServletResponse response) throws ParserConfigurationException, TransformerConfigurationException, TransformerException { try {/* ww w. ja v a 2 s .com*/ String workflow = request.getParameter("jsonnodeobj"); String linejson = request.getParameter("linejson"); String processId = request.getParameter("flowid"); String containerId = request.getParameter("containerId"); this.parentSplit = request.getParameter("parentSplit"); String path = ConfigReader.getinstance().get("workflowpath") + processId; path = path + System.getProperty("file.separator") + "Export"; File fdir = new File(path); if (!fdir.exists()) { fdir.mkdirs(); } File file = new File(fdir + System.getProperty("file.separator") + "bpmn.xpdl"); if (file.exists()) { file.delete(); } Writer output = new BufferedWriter(new FileWriter(file)); JSONObject jsonobj = new JSONObject(workflow); JSONObject json_line = new JSONObject(linejson); createDocument(); expwriteXml(jsonobj, containerId, processId, json_line); TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans1 = transfac.newTransformer(); trans1.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans1.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult kwcresult = new StreamResult(sw); DOMSource kwcsource = new DOMSource(dom); trans1.transform(kwcsource, kwcresult); output.write(sw.toString()); output.close(); /*response.setContentType("application/xml"); response.setHeader("Content-Disposition", "attachment; filename=export.xml"); OutputStream out = response.getOutputStream(); OutputFormat format = new OutputFormat(dom); format.setIndenting(true); format.setEncoding("UTF-8"); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(dom); out.close();*/ } catch (JSONException ex) { logger.warn(ex.getMessage(), ex); } catch (IOException ex) { logger.warn(ex.getMessage(), ex); } }
From source file:gov.nih.nci.cacis.common.util.ExtractSchematron.java
/** * Creates new instance of the schematron extractor. * /*w w w. j a va2 s. c o m*/ * @throws ClassCastException if there is an error getting a SchemaLoader * @throws ClassNotFoundException if there is an error getting a SchemaLoader * @throws InstantiationException if there is an error getting a SchemaLoader * @throws IllegalAccessException if there is an error getting a SchemaLoader * @throws TransformerFactoryConfigurationError if there is an error getting a serializer * @throws TransformerConfigurationException if there is an error getting a serializer * @throws XPathExpressionException if there is an error compiling expressions */ public ExtractSchematron() throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException, TransformerConfigurationException, TransformerFactoryConfigurationError, XPathExpressionException { final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader"); this.dfactory = DocumentBuilderFactory.newInstance(); this.dfactory.setNamespaceAware(true); final XPathFactory factory = XPathFactory.newInstance(); final XPath xpath = factory.newXPath(); xpath.setNamespaceContext(new SchNamespaceContext()); this.serializer = TransformerFactory.newInstance().newTransformer(); this.serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); this.patternExpr = xpath.compile("//sch:pattern"); this.schemaLoader = impl.createXSLoader(null); }
From source file:com.photon.phresco.impl.ConfigManagerImpl.java
public void writeXml(OutputStream fos) throws ConfigurationException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer;/* www. jav a2s . c o m*/ try { InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("strip-space.xsl"); StreamSource stream = new StreamSource(resourceAsStream); transformer = tFactory.newTransformer(stream); // transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); Source src = new DOMSource(document); Result res = new StreamResult(fos); transformer.transform(src, res); } catch (TransformerConfigurationException e) { throw new ConfigurationException(e); } catch (TransformerException e) { throw new ConfigurationException(e); } finally { if (fos != null) { Utility.closeStream(fos); } } }
From source file:com.isa.utiles.Utiles.java
public static String printDocument(Document doc) { try {//w w w .j av a 2 s. com TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; } catch (TransformerConfigurationException ex) { return ""; } catch (TransformerException ex) { return ""; } }