List of usage examples for org.dom4j.io XMLWriter close
public void close() throws IOException
From source file:com.beetle.framework.util.file.XMLReader.java
License:Apache License
public static void setProperties(String xmlFileName, String itemPath, String ElementName, String keyName, String valueName, String key, String value) { // ? ??????//from ww w . j a va 2s . c o m // ???? synchronized (writeLock) { SAXReader reader = new SAXReader(); XMLWriter writer = null; try { Document doc = reader.read(new File(xmlFileName)); Node node = doc.selectSingleNode(convertPath(itemPath)); if (node != null) { Iterator<?> it = node.selectNodes(ElementName).iterator(); while (it.hasNext()) { Element e = (Element) it.next(); if (e.attributeValue(keyName).equals(key)) { e.addAttribute(valueName, value); break; } } } writer = new XMLWriter(new FileWriter(xmlFileName)); writer.write(doc); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } reader = null; } } }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * XMLDocumentjava.io.Writer?//from ww w. j av a 2 s.c om * * * * ??Schema * * * * @param document * XML * @param outWriter * * * * * @param encoding * ? * @throws XMLDocException * * * * @throws BaseException * */ public static void toXML(Document document, java.io.Writer outWriter, String encoding) throws BaseException { // OutputFormat outformat = OutputFormat.createPrettyPrint(); if (encoding == null || encoding.trim().equals("")) { encoding = DEFAULT_ENCODING; } // ? outformat.setEncoding(encoding); XMLWriter xmlWriter = null; try { xmlWriter = new XMLWriter(outWriter, outformat); xmlWriter.write(document); xmlWriter.flush(); } catch (IOException ex) { throw new BaseException("UTIL-0001", ex); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException ex) { } } } }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * XMLDocumentjava.io.Writer?// w w w.ja v a 2s . c o m * * * * ??Schema * * * * @param document * XML * @param outStream * * * * * @param encoding * ? * @throws XMLDocException * * * * @throws BaseException * */ public static void toXML(Document document, java.io.OutputStream outStream, String encoding) throws BaseException { // OutputFormat outformat = new OutputFormat(); outformat.setIndentSize(0); outformat.setNewlines(true); outformat.setTrimText(true); // OutputFormat outformat = OutputFormat.createPrettyPrint(); if (encoding == null || encoding.trim().equals("")) { encoding = DEFAULT_ENCODING; } // ? outformat.setEncoding(encoding); XMLWriter xmlWriter = null; try { xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat); xmlWriter.write(document); xmlWriter.flush(); } catch (IOException ex) { throw new BaseException("UTIL-0001", ex); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException ex) { } } } }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
public static void element2XML(Element element, java.io.OutputStream outStream, String encoding) throws BaseException { ////w w w .jav a2 s . com OutputFormat outformat = new OutputFormat(); outformat.setIndentSize(0); outformat.setNewlines(false); outformat.setTrimText(true); // OutputFormat outformat = OutputFormat.createPrettyPrint(); if (encoding == null || encoding.trim().equals("")) { encoding = DEFAULT_ENCODING; } // ? outformat.setEncoding(encoding); XMLWriter xmlWriter = null; try { xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat); xmlWriter.write(element); xmlWriter.flush(); } catch (IOException ex) { throw new BaseException("UTIL-0001", ex); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException ex) { } } } }
From source file:com.brick.util.nciic.NciicUtil.java
/** * ?XML/* www . j av a 2s . c om*/ * @param text * @return * @throws Exception */ public static List<NciicEntity> readResult(String text) throws Exception { List<NciicEntity> resultList = new ArrayList<NciicEntity>(); NciicEntity result = null; Document d; XMLWriter writer = null; try { //SAXReader reader = new SAXReader(); //d = reader.read(new File("d:/test/testxml.xml")); d = DocumentHelper.parseText(text); String dateStr = DateUtil.dateToString(new Date(), "[yyyy-MM-dd][HH-mm]"); File xmlPath = new File(XML_PATH); if (!xmlPath.exists()) { xmlPath.mkdirs(); } File xpPath = new File(XP_PATH); if (!xpPath.exists()) { xpPath.mkdirs(); } writer = new XMLWriter(new FileOutputStream(new File(xmlPath, dateStr + ".xml"))); writer.write(d); writer.flush(); writer.close(); writer = null; Element root = d.getRootElement(); List<Element> allResult = root.elements("ROW"); Element input = null; List<Element> output = null; String result_msg = null; for (Element element : allResult) { result = new NciicEntity(); result_msg = null; input = element.element("INPUT"); result.setGmsfhm(input.element("gmsfhm").getText()); result.setXm(input.element("xm").getText()); output = element.element("OUTPUT").elements("ITEM"); for (Element out_element : output) { if (out_element.element("result_gmsfhm") != null) { result.setResult_gmsfhm(out_element.element("result_gmsfhm").getText()); } if (out_element.element("result_xm") != null) { result.setResult_xm(out_element.element("result_xm").getText()); } if (out_element.element("errormesage") != null) { result.setError_msg(out_element.element("errormesage").getText()); } if (out_element.element("errormesagecol") != null) { result.setError_msg_col(out_element.element("errormesagecol").getText()); } if (out_element.element("xp") != null) { result.setXp(out_element.element("xp").getText()); } if (!StringUtils.isEmpty(result.getXp())) { try { File f = new File(xpPath, result.getGmsfhm() + "-" + result.getXm() + ".jpg"); BufferedImage img = ImageIO.read( new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(result.getXp()))); ImageIO.write(img, "jpg", f); result.setXp_file(f.getPath()); } catch (IOException e) { e.printStackTrace(); } } } if ("".equals(result.getResult_gmsfhm()) && "".equals(result.getResult_xm())) { result_msg = ""; } else { if ("?".equals(result.getResult_gmsfhm())) { result_msg = "???"; } else if ("?".equals(result.getResult_xm())) { result_msg = "???"; } else if (!StringUtils.isEmpty(result.getError_msg())) { result_msg = result.getError_msg(); if (!StringUtils.isEmpty(result.getError_msg_col())) { result_msg += "(" + result.getError_msg_col() + ")"; } } } result.setResult_msg(result_msg); resultList.add(result); } return resultList; } catch (Exception e) { throw e; } finally { if (writer != null) { writer.flush(); writer.close(); writer = null; } } }
From source file:com.bsoft.baseframe.baseframe_utils.beanUtils.FileUtils.java
License:Open Source License
/** * ?XML?/*w w w . j av a2s . c o m*/ * @param document * @param file * @throws IOException */ public static boolean wrieteXML2Doc(Document document, File file) { boolean isCreate = false; try { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();// } if (!file.exists()) { isCreate = true; file.createNewFile();// java testData.java } else { isCreate = false; } //?XML?? if (isCreate) { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); // //document XMLWriter writer = new XMLWriter(new FileWriter(file), format); writer.write(document); writer.close(); } return true; } catch (Exception e) { return false; } }
From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java
License:Open Source License
public void output(Writer writer, RuleTemplateManager ruleTemplateManager) throws Exception { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(Constants.DEFAULT_CHARSET); XMLWriter xmlWriter = new XMLWriter(writer, format); xmlWriter.startDocument();/*from w ww . j a va2s . c o m*/ Element rootElement = DocumentHelper.createElement("RuleSet"); rootElement.addAttribute("version", ruleTemplateManager.getVersion()); xmlWriter.writeOpen(rootElement); OutputContext context = new OutputContext(); outputPackageInfos(xmlWriter, ruleTemplateManager, context); for (RuleTemplate ruleTemplate : ruleTemplateManager.getRuleTemplates()) { // PropertyDataType? // if (ruleTemplate.isAbstract() // && ruleTemplate.getSubRuleTemplates().length == 0) { // continue; // } outputRuleTemplate(xmlWriter, ruleTemplate, context); } xmlWriter.writeClose(rootElement); xmlWriter.endDocument(); xmlWriter.close(); }
From source file:com.bullx.demo.xml.XMLParser.java
License:Open Source License
public static void bookListToXML(List<Book> books) { Document document = DocumentHelper.createDocument(); // XMLbooks// w w w.j a v a 2s .c o m Element booksElement = document.addElement("books"); // booksElement.addComment("This is a test for dom4j, liubida, 2012.8.11"); for (Book book : books) { // Element bookElement = booksElement.addElement("book"); // : show bookElement.addAttribute("show", book.getShow() ? "yes" : "no"); // title bookElement.addElement("title").setText(book.getTitle()); // express bookElement.addElement("express").setText(book.getExpress()); } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); StringWriter out = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(out, format); try { xmlWriter.write(document); xmlWriter.flush(); String s = out.toString(); System.out.println(s); Log.info("xml done!"); } catch (Exception e) { Log.error("xml error!"); } finally { try { if (null != xmlWriter) { xmlWriter.close(); } if (null != out) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.bullx.utils.I2Util.java
License:Open Source License
public static String prettyXML(Document document) { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); StringWriter out = new StringWriter(); XMLWriter xmlWriter = new XMLWriter(out, format); try {//w ww . ja va 2s . c o m xmlWriter.write(document); xmlWriter.flush(); return out.toString(); } catch (Exception e) { Log.error(e.getMessage()); } finally { try { if (null != xmlWriter) { xmlWriter.close(); } if (null != out) { out.close(); } } catch (IOException e) { Log.error(e.getMessage()); } } return null; }
From source file:com.cc.framework.util.SettingUtils.java
License:Open Source License
/** * /*from www . j av a 2 s . com*/ * * @param setting * */ public static void set(Setting setting) { try { File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile(); Document document = new SAXReader().read(shopxxXmlFile); List<Element> elements = document.selectNodes("/shopxx/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(shopxxXmlFile); 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(); } }