List of usage examples for org.dom4j.io XMLWriter XMLWriter
public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException
From source file:com.alibaba.citrus.springext.util.ConvertToUnqualifiedStyle.java
License:Open Source License
private static void writeDocument(Document doc, OutputStream stream) throws IOException { String charset = "UTF-8"; Writer writer = new OutputStreamWriter(stream, charset); OutputFormat format = new OutputFormat(); format.setEncoding(charset);//from ww w . j a v a 2s. co m XMLWriter xmlWriter = new XMLWriter(writer, format); xmlWriter.write(doc); xmlWriter.flush(); }
From source file:com.amalto.workbench.utils.Util.java
License:Open Source License
public static String formatXsdSource(String xsdSource, boolean suppressDeclaration) { try {/*from w w w . j a va2s .c om*/ SAXReader reader = new SAXReader(); org.dom4j.Document document = reader.read(new StringReader(xsdSource)); StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");//$NON-NLS-1$ format.setSuppressDeclaration(suppressDeclaration); XMLWriter xmlwriter = new XMLWriter(writer, format); xmlwriter.write(document); String str = writer.toString(); writer.close(); xmlwriter.close(); return str; } catch (Exception e) { log.error(e.getMessage(), e); } return xsdSource; }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static void write(Document document, String filePath, String printMode, String encoding) throws IOException { OutputFormat format = null;//w ww.j a v a 2 s . c om if (printMode.toLowerCase().equals("pretty")) {//$NON-NLS-1$ // Pretty print the document format = OutputFormat.createPrettyPrint(); } else if (printMode.toLowerCase().equals("compact")) {//$NON-NLS-1$ // Compact format format = OutputFormat.createCompactFormat(); } format.setEncoding(encoding); // lets write to a file XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format); // XMLWriter logger = new XMLWriter( System.out, format ); writer.write(document); logger.info(Messages.bind(Messages.XmlUtil_Loginfo1, filePath)); // logger.write( document ); // logger.close(); writer.close(); }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static String format(Document document, OutputFormat format, String encoding) { StringWriter writer = new StringWriter(); format.setEncoding(encoding);//from w w w .j a v a 2s . c om format.setNewLineAfterDeclaration(false); // format.setSuppressDeclaration(suppressDeclaration); XMLWriter xmlwriter = new XMLWriter(writer, format); String result = ""; //$NON-NLS-1$ try { xmlwriter.write(document); result = writer.toString().replaceAll("<\\?xml.*?\\?>", "").trim();//$NON-NLS-1$//$NON-NLS-2$ } catch (Exception e) { log.error(e.getMessage(), e); } finally { try { if (xmlwriter != null) xmlwriter.close(); if (writer != null) writer.close(); } catch (IOException e) { } } return result; }
From source file:com.amalto.workbench.utils.XmlUtil.java
License:Open Source License
public static String formatXmlSource(String xmlSource) { SAXReader reader = new SAXReader(); StringReader stringReader = new StringReader(xmlSource); StringWriter writer = null;// ww w .jav a 2 s. com XMLWriter xmlwriter = null; String result = xmlSource; try { Document document = reader.read(stringReader); writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");//$NON-NLS-1$ format.setIndentSize(4); format.setSuppressDeclaration(true); xmlwriter = new XMLWriter(writer, format); xmlwriter.write(document); result = writer.toString(); } catch (Exception e) { } finally { try { if (stringReader != null) stringReader.close(); if (xmlwriter != null) xmlwriter.close(); if (writer != null) writer.close(); } catch (Exception e) { } } return result; }
From source file:com.anite.zebra.ext.xmlloader.XMLLoadProcess.java
License:Apache License
/** * Read in a file of XML, strip out the pretty printing via some juggling of objects and then return a * org.w3.document DOM object./*from www . j a va2s .c o m*/ * * @param xmlFile * The file to be read in. * @return A ready to use org.w3.Document with pretty printing stripped out. * @throws DocumentException * @throws MalformedURLException * @throws UnsupportedEncodingException * @throws IOException */ private Document readDocument(File xmlFile) throws DocumentException, MalformedURLException, UnsupportedEncodingException, IOException { SAXReader xmlReader = new SAXReader(); xmlReader.setStripWhitespaceText(true); org.dom4j.Document dom4jDocument = xmlReader.read(xmlFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputFormat format = OutputFormat.createCompactFormat(); XMLWriter writer = new XMLWriter(baos, format); writer.write(dom4jDocument); dom4jDocument = DocumentHelper.parseText(baos.toString()); DOMWriter domWriter = new DOMWriter(); Document doc = domWriter.write(dom4jDocument); return doc; }
From source file:com.app.util.SettingUtils.java
License:Open Source License
/** * /* w w w . j a v a 2 s . c o m*/ * * @param setting * */ public static void set(Setting setting) { try { File appXmlFile = new ClassPathResource(CommonAttributes.APP_XML_PATH).getFile(); Document document = new SAXReader().read(appXmlFile); List<Element> elements = document.selectNodes("/app/setting"); for (Element element : elements) { try { String name = element.attributeValue("name"); String value = beanUtils.getProperty(setting, name); Attribute attribute = element.attribute("value"); attribute.setValue(value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } FileOutputStream fileOutputStream = null; XMLWriter xmlWriter = null; try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndent(true); outputFormat.setIndent(" "); outputFormat.setNewlines(true); fileOutputStream = new FileOutputStream(appXmlFile); xmlWriter = new XMLWriter(fileOutputStream, outputFormat); xmlWriter.write(document); } catch (Exception e) { e.printStackTrace(); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException e) { } } IOUtils.closeQuietly(fileOutputStream); } Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME); cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.arc.mw.util.StateSaver.java
License:Open Source License
/** * Save the state of the root object into the given file. * @param out ascii stream to write to./* w w w . j av a2 s .c om*/ * @param object object whose state is to be saved. * @exception IOException error occurred in writing file. */ public static void saveState(Writer out, IXMLSavable object) throws IOException { DocumentFactory factory = DocumentFactory.getInstance(); Document document = factory.createDocument(); Element e = factory.createElement("root"); object.saveState(e); document.setRootElement(e); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.write(document); }
From source file:com.arthurh.xmergel.Xmergel.java
License:Open Source License
public void combine(List<Path> files) throws IOException, SAXException, TransformerException, DocumentException { Document finalDocument = this.saxReader.read(files.get(0).toFile()); for (Path file : files) { finalDocument = this.xdtTransformer.transform(finalDocument, this.saxReader.read(file.toFile())); }/*from ww w . java2s .com*/ OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(new FileWriter(this.resultFile.toFile()), format); xmlWriter.write(finalDocument); xmlWriter.close(); }
From source file:com.augmentum.common.util.XMLFormatter.java
License:Open Source License
public static String toString(Branch branch, String indent, boolean expandEmptyElements) throws IOException { ByteArrayMaker bam = new ByteArrayMaker(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setExpandEmptyElements(expandEmptyElements); format.setIndent(indent);/*from w w w . ja v a 2 s. c om*/ format.setLineSeparator("\n"); XMLWriter writer = new XMLWriter(bam, format); writer.write(branch); String content = bam.toString(StringPool.UTF8); // LEP-4257 //content = StringUtil.replace(content, "\n\n\n", "\n\n"); if (content.endsWith("\n\n")) { content = content.substring(0, content.length() - 2); } if (content.endsWith("\n")) { content = content.substring(0, content.length() - 1); } while (content.indexOf(" \n") != -1) { content = StringUtil.replace(content, " \n", "\n"); } return content; }