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:com.mirth.connect.model.util.ImportConverter.java
public static Document convertServerConfiguration(String serverConfiguration) throws Exception { serverConfiguration = convertPackageNames(serverConfiguration); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document;/*from w w w . java 2s .c om*/ DocumentBuilder builder; builder = factory.newDocumentBuilder(); document = builder.parse(new InputSource(new StringReader(serverConfiguration))); // Remove users from the server configuration file if they were there. Element documentElement = document.getDocumentElement(); NodeList users = documentElement.getElementsByTagName("users"); if (users != null && users.getLength() > 0) { documentElement.removeChild(users.item(0)); } Element channelsRoot = (Element) document.getElementsByTagName("channels").item(0); NodeList channels = getElements(document, "channel", "com.mirth.connect.model.Channel"); List<Element> channelList = new ArrayList<Element>(); int length = channels.getLength(); for (int i = 0; i < length; i++) { // Must get node 0 because the first channel is removed each // iteration Element channel = (Element) channels.item(0); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(channel), new StreamResult(sw)); String channelDocXML = sw.toString(); channelList.add(new DonkeyElement(convertChannelString(channelDocXML)).getElement()); channelsRoot.removeChild(channel); } for (Element channel : channelList) { channelsRoot.appendChild(document.importNode(channel, true)); } NodeList codeTemplates = documentElement.getElementsByTagName("codeTemplates"); int codeTemplateCount = codeTemplates.getLength(); for (int i = 0; i < codeTemplateCount; i++) { Element codeTemplate = (Element) codeTemplates.item(i); Element convertedCodeTemplate = convertCodeTemplates(new DonkeyElement(codeTemplate).toXml()) .getDocumentElement(); documentElement.replaceChild(document.importNode(convertedCodeTemplate, true), codeTemplate); } return document; }
From source file:com.bcmcgroup.flare.client.ClientUtil.java
/** * Convert a Document into a String// w w w . j a va 2s.com * * @param document the Document to be converted to String * @param omitXmlDeclaration set to true if you'd like to omit the XML declaration, false otherwise * @return the String converted from a Document * */ public static String convertDocumentToString(Document document, boolean omitXmlDeclaration) { try { StringWriter stringWriter = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer transformer = transformerFactory.newTransformer(); if (omitXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (TransformerException e) { logger.error("Transformer Exception when attempting to convert a document to a string. "); } return null; }
From source file:org.lareferencia.backend.indexer.IntelligoIndexer.java
private Transformer buildTransformer() throws IndexerException { Transformer trf;/* ww w. ja va 2 s . com*/ try { StreamSource stylesource = new StreamSource(stylesheet); trf = xformFactory.newTransformer(stylesource); trf = MedatadaDOMHelper.buildXSLTTransformer(stylesheet); trf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trf.setOutputProperty(OutputKeys.INDENT, "yes"); trf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); } catch (TransformerConfigurationException e) { throw new IndexerException(e.getMessage(), e.getCause()); } return trf; }
From source file:ApplyXPathJAXP.java
static void printNodeList(NodeList nodelist) throws Exception { Node n;/* w w w. ja v a 2s . c o m*/ // Set up an identity transformer to use as serializer. Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); for (int i = 0; i < nodelist.getLength(); i++) { n = nodelist.item(i); if (isTextNode(n)) { // DOM may have more than one node corresponding to a // single XPath text node. Coalesce all contiguous text nodes // at this level StringBuffer sb = new StringBuffer(n.getNodeValue()); for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) { sb.append(nn.getNodeValue()); } System.out.print(sb); } else { serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out))); } System.out.println(); } }
From source file:Main.java
private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone, final Map<Thread, Transformer> threadMap, final String xslURL) throws TransformerConfigurationException { final Thread currentThread = Thread.currentThread(); Transformer transformer = null; if (threadMap != null) { transformer = threadMap.get(currentThread); }/*from ww w. j a va 2s .c o m*/ if (transformer == null) { if (xslURL == null) { transformer = tFactory.newTransformer(); // "never null" } else { transformer = tFactory.newTransformer(new StreamSource(xslURL)); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (threadMap != null) { threadMap.put(currentThread, transformer); } } transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); return transformer; }
From source file:uk.ac.diamond.shibbolethecpauthclient.Utils.java
/** * Helper method that turns the message into a formatted XML string. * // w w w .j a va 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:elaborate.editor.export.tei.TeiMaker.java
public String toXML() { if (tei == null) { return null; }// ww w. ja v a 2 s. com TransformerFactory transfac = TransformerFactory.newInstance(); try { DOMSource source = new DOMSource(tei); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); trans.transform(source, result); return sw.toString().replace("interpgrp>", "interpGrp>").replaceAll(" +<lb/>", "<lb/>"); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:me.cavar.pg2tei.TEIDoc.java
/** * * @param doc//w w w . jav a 2s .c o m * @return */ public String getTEIXMLString(Document doc) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; StringWriter strw = new StringWriter(); StreamResult result = new StreamResult(strw); try { trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // create string from xml tree DOMSource source = new DOMSource(doc); trans.transform(source, result); } catch (TransformerConfigurationException ex) { Logger.getLogger(TEIDoc.class.getName()).log(Level.SEVERE, null, ex); } catch (TransformerException ex) { Logger.getLogger(TEIDoc.class.getName()).log(Level.SEVERE, null, ex); } return strw.toString(); }
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 *///from w w w .j a va 2s. 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:com.hangum.tadpole.engine.sql.util.export.XMLExporter.java
/** * make content file/*from w w w. j a v a 2 s . c o m*/ * * @param tableName * @param rsDAO * @return * @throws Exception */ public static String makeContentFile(String tableName, QueryExecuteResultDTO rsDAO) throws Exception { String strTmpDir = PublicTadpoleDefine.TEMP_DIR + tableName + System.currentTimeMillis() + PublicTadpoleDefine.DIR_SEPARATOR; String strFile = tableName + ".xml"; String strFullPath = strTmpDir + strFile; final StringWriter stWriter = new StringWriter(); final List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element results = doc.createElement(tableName); doc.appendChild(results); Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName(); for (int i = 0; i < dataList.size(); i++) { Map<Integer, Object> mapColumns = dataList.get(i); Element row = doc.createElement("Row"); results.appendChild(row); for (int j = 1; j < mapColumns.size(); j++) { String columnName = mapLabelName.get(j); String strValue = mapColumns.get(j) == null ? "" : "" + mapColumns.get(j); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(strValue)); row.appendChild(node); } } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 4); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//"ISO-8859-1"); StreamResult sr = new StreamResult(stWriter); transformer.transform(domSource, sr); FileUtils.writeStringToFile(new File(strFullPath), stWriter.toString(), true); return strFullPath; }