Java Code Examples for org.dom4j.io.XMLWriter#close()
The following examples show how to use
org.dom4j.io.XMLWriter#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Dom4JParser.java From tutorials with MIT License | 6 votes |
public void generateNewDocument() { try { Document document = DocumentHelper.createDocument(); Element root = document.addElement("XMLTutorials"); Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01"); tutorialElement.addAttribute("type", "xml"); tutorialElement.addElement("title").addText("XML with Dom4J"); tutorialElement.addElement("description").addText("XML handling with Dom4J"); tutorialElement.addElement("date").addText("14/06/2016"); tutorialElement.addElement("author").addText("Dom4J tech writer"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_dom4j_new.xml")), format); writer.write(document); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Example 2
Source File: ConfigManage.java From oim-fx with MIT License | 6 votes |
public static boolean saveXml(String path, Document document) { boolean b = false; try { OnlyFileUtil.checkOrCreateFile(path); File xmlFile = new File(path); FileWriter fileWriter = new FileWriter(xmlFile); XMLWriter writer = new XMLWriter(fileWriter); writer.write(document); writer.close(); b = true; documentMap.put(path, document); } catch (IOException e) { e.printStackTrace(); } return b; }
Example 3
Source File: Command.java From ApkCustomizationTool with Apache License 2.0 | 6 votes |
/** * 修改strings.xml文件内容 * * @param file strings文件 * @param strings 修改的值列表 */ private void updateStrings(File file, List<Strings> strings) { try { if (strings == null || strings.isEmpty()) { return; } Document document = new SAXReader().read(file); List<Element> elements = document.getRootElement().elements(); elements.forEach(element -> { final String name = element.attribute("name").getValue(); strings.forEach(s -> { if (s.getName().equals(name)) { element.setText(s.getValue()); callback("修改 strings.xml name='" + name + "' value='" + s.getValue() + "'"); } }); }); XMLWriter writer = new XMLWriter(new FileOutputStream(file)); writer.write(document); writer.close(); } catch (Exception e) { e.printStackTrace(); } }
Example 4
Source File: EnhanceBaseBuilder.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
private static File createPernsistenceXml(List<Class<?>> classes, File directory) throws Exception { Document document = DocumentHelper.createDocument(); Element persistence = document.addElement("persistence", "http://java.sun.com/xml/ns/persistence"); persistence.addAttribute(QName.get("schemaLocation", "xsi", "http://www.w3.org/2001/XMLSchema-instance"), "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"); persistence.addAttribute("version", "2.0"); Element unit = persistence.addElement("persistence-unit"); unit.addAttribute("name", "enhance"); for (Class<?> o : classes) { Element element = unit.addElement("class"); element.addText(o.getCanonicalName()); } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); File file = new File(directory, "persistence.xml"); XMLWriter writer = new XMLWriter(new FileWriter(file), format); writer.write(document); writer.close(); return file; }
Example 5
Source File: XSLConfiguration.java From CogniCrypt with Eclipse Public License 2.0 | 6 votes |
@Override public File persistConf() throws IOException { final XMLClaferParser parser = new XMLClaferParser(); Document configInXMLFormat = parser.displayInstanceValues(instance, this.options); if (configInXMLFormat != null) { final OutputFormat format = OutputFormat.createPrettyPrint(); final XMLWriter writer = new XMLWriter(new FileWriter(pathOnDisk), format); writer.write(configInXMLFormat); writer.close(); configInXMLFormat = null; return new File(pathOnDisk); } else { Activator.getDefault().logError(Constants.NO_XML_INSTANCE_FILE_TO_WRITE); } return null; }
Example 6
Source File: SetupDBResource.java From dal with Apache License 2.0 | 5 votes |
private boolean initializeDatasourceXml(String dbaddress, String dbport, String dbuser, String dbpassword, String dbcatalog) throws Exception { boolean result = false; try { String connectionUrl = String.format(jdbcUrlTemplate, dbaddress, dbport, dbcatalog); Document document = DocumentHelper.createDocument(); Element root = document.addElement("Datasources"); root.addElement("Datasource").addAttribute(DATASOURCE_NAME, LOGIC_DBNAME) .addAttribute(DATASOURCE_USERNAME, dbuser).addAttribute(DATASOURCE_PASSWORD, dbpassword) .addAttribute(DATASOURCE_CONNECTION_URL, connectionUrl) .addAttribute(DATASOURCE_DRIVER_CLASS, DATASOURCE_MYSQL_DRIVER); URL url = classLoader.getResource(WEB_XML); String path = url.getPath().replace(WEB_XML, DATASOURCE_XML); try (FileWriter fileWriter = new FileWriter(path)) { XMLWriter writer = new XMLWriter(fileWriter); writer.write(document); writer.close(); } DalClientFactory.getClient(LOGIC_DBNAME); result = true; } catch (Throwable e) { throw e; } return result; }
Example 7
Source File: XmlUtil.java From SpringBootUnity with MIT License | 5 votes |
/** * 保存文档 * * @throws Exception */ public static void save(Document doc, String xmlPath, String encoding) throws Exception { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(encoding); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xmlPath), encoding), format); writer.write(doc); writer.flush(); writer.close(); }
Example 8
Source File: LianlianBranchTest.java From aaden-pay with Apache License 2.0 | 5 votes |
private void writeXml(List<JSONArray> list) throws Exception { File file = new File(SAVE_PATH); if (file.exists()) file.delete(); // 生成一个文档 Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); for (JSONArray jsonArray : list) { for (Object object : jsonArray) { JSONObject json = (JSONObject) object; System.out.println(json); Element element = root.addElement("branch"); // 为cdr设置属性名和属性值 element.addAttribute("branchId", json.getString("prcptcd").trim());// 支行行号 element.addAttribute("bankCode", json.getString("bankCode").trim());// 银行类型 element.addAttribute("cityCode", json.getString("cityCode").trim());// 城市代码 element.addAttribute("branchName", json.getString("brabank_name").trim());// 支行名称 } } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(SAVE_PATH)), "UTF-8"), format); // 写入新文件 writer.write(document); writer.flush(); writer.close(); }
Example 9
Source File: AddMessages.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@SuppressFBWarnings("DM_EXIT") public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: " + AddMessages.class.getName() + " <input collection> <output collection>"); System.exit(1); } // Load plugins, in order to get message files DetectorFactoryCollection.instance(); String inputFile = args[0]; String outputFile = args[1]; Project project = new Project(); SortedBugCollection inputCollection = new SortedBugCollection(project); inputCollection.readXML(inputFile); Document document = inputCollection.toDocument(); AddMessages addMessages = new AddMessages(inputCollection, document); addMessages.execute(); XMLWriter writer = new XMLWriter(new BufferedOutputStream(new FileOutputStream(outputFile)), OutputFormat.createPrettyPrint()); writer.write(document); writer.close(); }
Example 10
Source File: XmlUtil.java From SpringBootUnity with MIT License | 5 votes |
/** * xml转换为字符串 * * @param doc * @param encoding * @return * @throws Exception */ public static String toString(Document doc, String encoding) throws Exception { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(encoding); ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(new OutputStreamWriter(byteOS, encoding), format); writer.write(doc); writer.flush(); writer.close(); return byteOS.toString(encoding); }
Example 11
Source File: ImportFileUpdater.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Updates the passed import file into the equivalent 1.4 format. * * @param source the source import file * @param destination the destination import file */ public void updateImportFile(String source, String destination) { XmlPullParser reader = getReader(source); XMLWriter writer = getWriter(destination); this.shownWarning = false; try { // Start the documentation writer.startDocument(); // Start reading the document int eventType = reader.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { ImportFileUpdater.this.outputCurrentElement(reader, writer, new OutputChildren()); } eventType = reader.next(); } // End and close the document writer.endDocument(); writer.close(); } catch (Exception exception) { throw new AlfrescoRuntimeException("Unable to update import file.", exception); } }
Example 12
Source File: PersistenceXmlHelper.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public static List<String> directWrite(String path, List<String> classNames) throws Exception { try { Document document = DocumentHelper.createDocument(); Element persistence = document.addElement("persistence", "http://java.sun.com/xml/ns/persistence"); persistence.addAttribute(QName.get("schemaLocation", "xsi", "http://www.w3.org/2001/XMLSchema-instance"), "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"); persistence.addAttribute("version", "2.0"); for (String className : classNames) { Element unit = persistence.addElement("persistence-unit"); unit.addAttribute("name", className); unit.addAttribute("transaction-type", "RESOURCE_LOCAL"); Element provider = unit.addElement("provider"); provider.addText(PersistenceProviderImpl.class.getName()); Element mapped_element = unit.addElement("class"); mapped_element.addText(className); Element sliceJpaObject_element = unit.addElement("class"); sliceJpaObject_element.addText("com.x.base.core.entity.SliceJpaObject"); Element jpaObject_element = unit.addElement("class"); jpaObject_element.addText("com.x.base.core.entity.JpaObject"); } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); File file = new File(path); FileUtils.touch(file); XMLWriter writer = new XMLWriter(new FileWriter(file), format); writer.write(document); writer.close(); return classNames; } catch (Exception e) { throw new Exception("registContainerEntity error.className:" + ListTools.toStringJoin(classNames), e); } }
Example 13
Source File: PersistenceXmlWriter.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
private static void write(Argument arg) throws Exception { try { Document document = DocumentHelper.createDocument(); Element persistence = document.addElement("persistence", "http://java.sun.com/xml/ns/persistence"); persistence.addAttribute(QName.get("schemaLocation", "xsi", "http://www.w3.org/2001/XMLSchema-instance"), "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"); persistence.addAttribute("version", "2.0"); for (Class<?> cls : scanContainerEntity(arg.getProject())) { Element unit = persistence.addElement("persistence-unit"); unit.addAttribute("name", cls.getCanonicalName()); unit.addAttribute("transaction-type", "RESOURCE_LOCAL"); Element provider = unit.addElement("provider"); provider.addText(PersistenceProviderImpl.class.getCanonicalName()); for (Class<?> o : scanMappedSuperclass(cls)) { Element mapped_element = unit.addElement("class"); mapped_element.addText(o.getCanonicalName()); } Element slice_unit_properties = unit.addElement("properties"); Map<String, String> properties = new LinkedHashMap<String, String>(); for (Entry<String, String> entry : properties.entrySet()) { Element property = slice_unit_properties.addElement("property"); property.addAttribute("name", entry.getKey()); property.addAttribute("value", entry.getValue()); } } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); File file = new File(arg.getPath()); XMLWriter writer = new XMLWriter(new FileWriter(file), format); writer.write(document); writer.close(); System.out.println("create persistence.xml at path:" + arg.getPath()); } catch (Exception e) { e.printStackTrace(); } }
Example 14
Source File: XmlBlobType.java From unitime with Apache License 2.0 | 5 votes |
public void nullSafeSet(PreparedStatement ps, Object value, int index, SessionImplementor session) throws SQLException, HibernateException { if (value == null) { ps.setNull(index, sqlTypes()[0]); } else { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(new GZIPOutputStream(bytes),OutputFormat.createCompactFormat()); writer.write((Document)value); writer.flush(); writer.close(); ps.setBinaryStream(index, new ByteArrayInputStream(bytes.toByteArray(),0,bytes.size()), bytes.size()); } catch (IOException e) { throw new HibernateException(e.getMessage(),e); } } }
Example 15
Source File: XmlClobType.java From unitime with Apache License 2.0 | 5 votes |
public void nullSafeSet(PreparedStatement ps, Object value, int index, SessionImplementor session) throws SQLException, HibernateException { if (value == null) { ps.setNull(index, sqlTypes()[0]); } else { try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(bytes,OutputFormat.createCompactFormat()); writer.write((Document)value); writer.flush(); writer.close(); ps.setCharacterStream(index, new CharArrayReader(bytes.toString().toCharArray(),0,bytes.size()), bytes.size()); } catch (IOException e) { throw new HibernateException(e.getMessage(),e); } } }
Example 16
Source File: Config.java From PatatiumWebUi with Apache License 2.0 | 4 votes |
public static void SetXML(String Base_Url,String UserName,String PassWord,String driver,String nodeURL,String Recipients,String ReportUrl,String LogUrl) throws IOException, DocumentException { File file = new File(path); if (!file.exists()) { throw new IOException("Can't find " + path); } SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); for (Iterator<?> i = root.elementIterator(); i.hasNext();) { Element page = (Element) i.next(); if(page.attributeCount()>0) { if (page.attribute(0).getValue().equalsIgnoreCase("Base_Url")) { page.attribute(1).setValue(Base_Url); //System.out.println(page.attribute(1).getValue()); } if (page.attribute(0).getValue().equalsIgnoreCase("UserName")) { page.attribute(1).setValue(UserName); } if (page.attribute(0).getValue().equalsIgnoreCase("PassWord")) { page.attribute(1).setValue(PassWord); } if (page.attribute(0).getValue().equalsIgnoreCase("driver")) { page.attribute(1).setValue(driver); } if (page.attribute(0).getValue().equalsIgnoreCase("nodeURL")) { page.attribute(1).setValue("http://"+nodeURL+"/wd/hub"); } if (page.attribute(0).getValue().equalsIgnoreCase("Recipients")) { page.attribute(1).setValue(Recipients); } if (page.attribute(0).getValue().equalsIgnoreCase("ReportUrl")) { page.attribute(1).setValue(ReportUrl); } if (page.attribute(0).getValue().equalsIgnoreCase("LogUrl")) { page.attribute(1).setValue(LogUrl); } continue; } //continue; } if (driver.equalsIgnoreCase("FirefoxDriver")) { driver="火狐浏览器"; } else if (driver.equalsIgnoreCase("ChormeDriver")) { driver="谷歌浏览器"; } else if(driver.equalsIgnoreCase("InternetExplorerDriver-8")) { driver="IE8浏览器"; } else if(driver.equalsIgnoreCase("InternetExplorerDriver-9")) { driver="IE9浏览器"; } else { driver="火狐浏览器"; } try{ /** 格式化输出,类型IE浏览一样 */ OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML编码 */ format.setEncoding("gb2312"); /** 将document中的内容写入文件中 */ XMLWriter writer = new XMLWriter(new FileWriter(new File(path)),format); writer.write(document); writer.close(); /** 执行成功,需返回1 */ int returnValue = 1; System.out.println("设置配置环境:"+Base_Url + ";用户名:"+UserName + "密码:" + PassWord +"浏览器:" +driver +"成功!"); System.out.println("设置报表url:"+ReportUrl); System.out.println("设置报表日志:"+LogUrl); System.out.println("设置收件人地址:"+Recipients); }catch(Exception ex){ ex.printStackTrace(); System.out.println("设置配置环境:"+Base_Url + ";用户名:"+UserName + "密码:" + PassWord +"浏览器:" +driver +"失败!"); } }
Example 17
Source File: Command.java From ApkCustomizationTool with Apache License 2.0 | 4 votes |
/** * 修改AndroidManifest.xml文件 * * @param appFolderName AndroidManifest.xml文件所在路径 * @param manifest 要修改的信息 */ public boolean updateAndroidManifest(String appFolderName, Manifest manifest) { if (manifest == null) { return false; } try { File androidManifestFile = new File(appFolderName + File.separator + "AndroidManifest.xml"); Document document = new SAXReader().read(androidManifestFile); Element element = document.getRootElement().element("application"); List<Element> list = element.elements("meta-data"); List<MetaData> metaData = manifest.getMetaData(); boolean isUpdate = false; for (MetaData data : metaData) { String name = data.getName(); String value = data.getValue(); for (Element s : list) { Attribute attribute = s.attribute("name"); if (attribute.getValue().equals(name)) { s.attribute("value").setValue(value); isUpdate = true; callback("更新 AndroidManifest.xml meta-data name='" + attribute.getValue() + "' value='" + value + "'"); } } } // OutputFormat format = OutputFormat.createCompactFormat(); // format.setNewlines(true); // format.setTrimText(true); // format.setIndent(true); // format.setIndentSize(4); // XMLWriter writer = new XMLWriter(new FileOutputStream(androidManifestFile), format); if(isUpdate){ XMLWriter writer = new XMLWriter(new FileOutputStream(androidManifestFile)); writer.write(document); writer.close(); callback("更新 AndroidManifest.xml 完成 ~ "); } } catch (Exception e) { e.printStackTrace(); return false; } return true; }
Example 18
Source File: Dom4jUtil2.java From egdownloader with GNU General Public License v2.0 | 4 votes |
public static void writeDOM2XML(File file, Document doc) throws Exception{ OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format); writer.write(doc); writer.close(); }
Example 19
Source File: CommonDao.java From jeewx with Apache License 2.0 | 4 votes |
/** * 生成XML importFile 导出xml工具类 */ public HttpServletResponse createXml(ImportFile importFile) { HttpServletResponse response = importFile.getResponse(); HttpServletRequest request = importFile.getRequest(); try { // 创建document对象 Document document = DocumentHelper.createDocument(); document.setXMLEncoding("UTF-8"); // 创建根节点 String rootname = importFile.getEntityName() + "s"; Element rElement = document.addElement(rootname); Class entityClass = importFile.getEntityClass(); String[] fields = importFile.getField().split(","); // 得到导出对象的集合 List objList = loadAll(entityClass); Class classType = entityClass.getClass(); for (Object t : objList) { Element childElement = rElement.addElement(importFile.getEntityName()); for (int i = 0; i < fields.length; i++) { String fieldName = fields[i]; // 第一为实体的主键 if (i == 0) { childElement.addAttribute(fieldName, String.valueOf(TagUtil.fieldNametoValues(fieldName, t))); } else { Element name = childElement.addElement(fieldName); name.setText(String.valueOf(TagUtil.fieldNametoValues(fieldName, t))); } } } String ctxPath = request.getSession().getServletContext().getRealPath(""); File fileWriter = new File(ctxPath + "/" + importFile.getFileName()); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(fileWriter)); xmlWriter.write(document); xmlWriter.close(); // 下载生成的XML文件 UploadFile uploadFile = new UploadFile(request, response); uploadFile.setRealPath(importFile.getFileName()); uploadFile.setTitleField(importFile.getFileName()); uploadFile.setExtend("bak"); viewOrDownloadFile(uploadFile); } catch (Exception e) { e.printStackTrace(); } return response; }
Example 20
Source File: XmlUtil.java From mybatis-daoj with Apache License 2.0 | 3 votes |
/** * 将XML文档写入指定的文件. * * @param document XML文档 * @param fileName 文件名 * @param format 输出格式, 参考常量XmlUtil.FORMAT_GBK_PRETTY, * XmlUtil.FORMAT_UTF_PRETTY等 * @throws java.io.IOException */ public static void write(Document document, String fileName, OutputFormat format) throws IOException { XMLWriter writer = new XMLWriter(new FileWriter(fileName), format); writer.write(document); writer.flush(); writer.close(); }