Java Code Examples for org.dom4j.io.OutputFormat#setEncoding()
The following examples show how to use
org.dom4j.io.OutputFormat#setEncoding() .
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: 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 2
Source File: XMLTransferReportWriter.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * Start the transfer report */ public void startTransferReport(String encoding, Writer writer) throws SAXException { OutputFormat format = OutputFormat.createPrettyPrint(); format.setNewLineAfterDeclaration(false); format.setIndentSize(3); format.setEncoding(encoding); this.writer = new XMLWriter(writer, format); this.writer.startDocument(); this.writer.startPrefixMapping(PREFIX, TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI); // Start Transfer Manifest // uri, name, prefix this.writer.startElement(TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI, TransferReportModel.LOCALNAME_TRANSFER_REPORT, PREFIX + ":" + TransferReportModel.LOCALNAME_TRANSFER_REPORT, EMPTY_ATTRIBUTES); }
Example 3
Source File: ImportFileUpdater.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * Get the writer for the import file * * @param destination the destination XML import file * @return the XML writer */ private XMLWriter getWriter(String destination) { try { // Define output format OutputFormat format = OutputFormat.createPrettyPrint(); format.setNewLineAfterDeclaration(false); format.setIndentSize(INDENT_SIZE); format.setEncoding(this.fileEncoding); return new XMLWriter(new FileOutputStream(destination), format); } catch (Exception exception) { throw new AlfrescoRuntimeException("Unable to create XML writer.", exception); } }
Example 4
Source File: WriteXml.java From Crawer with MIT License | 6 votes |
/** * 写数据到xml * * @param datas * @throws Exception */ public static void witeXml(@SuppressWarnings("rawtypes") Map map,String xmlFilePath) throws Exception { Document doc = DomHelper.createDomFJ(); doc.addComment("以utf-8的编码"); Element books = DomHelper.appendChile("books", doc); /* 格式化输出 */ OutputFormat format = OutputFormat.createPrettyPrint();// 紧缩 format.setEncoding("utf-8"); // 设置utf-8编码 Element book = DomHelper.appendChile("book", books); //构建树形结构 rescue(book, map); //输出 FileOutputStream fos = new FileOutputStream(xmlFilePath); XMLWriter writer=new XMLWriter(fos,format); //写结构 writer.write(doc); //关闭流 writer.close(); }
Example 5
Source File: StringUtil.java From jeewx with Apache License 2.0 | 5 votes |
/** * html 必须是格式良好的 * @param str * @return * @throws Exception */ public static String formatHtml(String str) throws Exception { Document document = null; document = DocumentHelper.parseText(str); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); StringWriter writer = new StringWriter(); HTMLWriter htmlWriter = new HTMLWriter(writer, format); htmlWriter.write(document); htmlWriter.close(); return writer.toString(); }
Example 6
Source File: XmlExport.java From openmeetings with Apache License 2.0 | 5 votes |
public static void toXml(Writer out, Document doc) throws IOException { OutputFormat outformat = OutputFormat.createPrettyPrint(); outformat.setIndentSize(1); outformat.setIndent("\t"); outformat.setEncoding(UTF_8.name()); XMLWriter writer = new XMLWriter(out, outformat); writer.write(doc); writer.flush(); out.flush(); out.close(); }
Example 7
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 8
Source File: AbstractReferenceUpdater.java From urule with Apache License 2.0 | 5 votes |
protected String xmlToString(Document doc){ StringWriter stringWriter = new StringWriter(); OutputFormat xmlFormat = new OutputFormat(); xmlFormat.setEncoding("UTF-8"); XMLWriter xmlWriter = new XMLWriter(stringWriter, xmlFormat); try { xmlWriter.write(doc); xmlWriter.close(); return stringWriter.toString(); } catch (IOException e) { e.printStackTrace(); throw new RuleException(e); } }
Example 9
Source File: ExportSourceImporter.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
private XMLWriter createXMLExporter(Writer writer) { // Define output format OutputFormat format = OutputFormat.createPrettyPrint(); format.setNewLineAfterDeclaration(false); format.setIndentSize(3); format.setEncoding("UTF-8"); // Construct an XML Exporter XMLWriter xmlWriter = new XMLWriter(writer, format); return xmlWriter; }
Example 10
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 11
Source File: StringUtil.java From jeecg with Apache License 2.0 | 5 votes |
/** * html 必须是格式良好的 * @param str * @return * @throws Exception */ public static String formatHtml(String str) throws Exception { Document document = null; document = DocumentHelper.parseText(str); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); StringWriter writer = new StringWriter(); HTMLWriter htmlWriter = new HTMLWriter(writer, format); htmlWriter.write(document); htmlWriter.close(); return writer.toString(); }
Example 12
Source File: DataDstXml.java From xresloader with MIT License | 5 votes |
/** * 转储常量数据 * * @return 常量数据,不支持的时候返回空 */ public final byte[] dumpConst(HashMap<String, Object> data) throws ConvException, IOException { // pretty print OutputFormat of = null; if (ProgramOptions.getInstance().prettyIndent <= 0) { of = OutputFormat.createCompactFormat(); } else { of = OutputFormat.createPrettyPrint(); of.setIndentSize(ProgramOptions.getInstance().prettyIndent); } // build xml tree Document doc = DocumentHelper.createDocument(); String encoding = SchemeConf.getInstance().getKey().getEncoding(); if (null != encoding && false == encoding.isEmpty()) { doc.setXMLEncoding(encoding); of.setEncoding(encoding); } doc.setRootElement(DocumentHelper.createElement(ProgramOptions.getInstance().xmlRootName)); doc.getRootElement().addComment("this file is generated by xresloader, please don't edit it."); writeData(doc.getRootElement(), data, ""); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(bos, of); writer.write(doc); return bos.toByteArray(); }
Example 13
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 14
Source File: TestCase.java From FoxBPM with Apache License 2.0 | 5 votes |
public void testB() { BpmnXMLConverter converter = new BpmnXMLConverter(); Document doc = converter.convertToXML(null); try { // 定义输出流的目的地 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); // 定义用于输出xml文件的XMLWriter对象 XMLWriter xmlWriter = new XMLWriter(System.out, format); xmlWriter.write(doc); xmlWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
Example 15
Source File: XmlUtil.java From stategen with GNU Affero General Public License v3.0 | 5 votes |
public static void saveDocument(Document document, File xmlFile) throws IOException { @Cleanup Writer osWrite = new OutputStreamWriter(new FileOutputStream(xmlFile),StringUtil.UTF_8);// 创建输出流 OutputFormat format = OutputFormat.createPrettyPrint(); // 获取输出的指定格式 format.setEncoding(document.getXMLEncoding()); format.setNewLineAfterDeclaration(false);//解决声明下空行问题 format.setEncoding(StringUtil.UTF_8);// 设置编码 ,确保解析的xml为UTF-8格式 @Cleanup XMLWriter writer = new XMLWriter(osWrite, format);// XMLWriter writer.write(document);// 把document写入xmlFile指定的文件(可以为被解析的文件或者新创建的文件) writer.flush(); }
Example 16
Source File: DataDstXml.java From xresloader with MIT License | 4 votes |
@Override public final byte[] build(DataDstImpl compiler) throws ConvException { // pretty print OutputFormat of = null; if (ProgramOptions.getInstance().prettyIndent <= 0) { of = OutputFormat.createCompactFormat(); } else { of = OutputFormat.createPrettyPrint(); of.setIndentSize(ProgramOptions.getInstance().prettyIndent); } // build data DataDstObject data_obj = build_data(compiler); // build xml tree Document doc = DocumentHelper.createDocument(); String encoding = SchemeConf.getInstance().getKey().getEncoding(); if (null != encoding && false == encoding.isEmpty()) { doc.setXMLEncoding(encoding); of.setEncoding(encoding); } doc.setRootElement(DocumentHelper.createElement(ProgramOptions.getInstance().xmlRootName)); doc.getRootElement().addComment("this file is generated by xresloader, please don't edit it."); Element header = DocumentHelper.createElement("header"); Element body = DocumentHelper.createElement("body"); Element data_message_type = DocumentHelper.createElement("data_message_type"); writeData(header, data_obj.header, header.getName()); // body for (Map.Entry<String, List<Object>> item : data_obj.body.entrySet()) { for (Object obj : item.getValue()) { Element xml_item = DocumentHelper.createElement(item.getKey()); writeData(xml_item, obj, item.getKey()); body.add(xml_item); } } writeData(body, data_obj.body, body.getName()); writeData(data_message_type, data_obj.data_message_type, data_message_type.getName()); doc.getRootElement().add(header); doc.getRootElement().add(body); doc.getRootElement().add(data_message_type); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(bos, of); writer.write(doc); return bos.toByteArray(); } catch (Exception e) { this.logErrorMessage("write xml failed, %s", e.getMessage()); return null; } }
Example 17
Source File: WFGraph.java From EasyML with Apache License 2.0 | 4 votes |
/** * Transform the Graph into an workflow xml definition * @param jobname the job name of Oozie job, can't be null * @return workflow xml */ public String toWorkflow(String jobname) { Namespace xmlns = new Namespace("", "uri:oozie:workflow:0.4"); // root namespace uri QName qName = QName.get("workflow-app", xmlns); // your root element's name Element workflow = DocumentHelper.createElement(qName); Document xmldoc = DocumentHelper.createDocument(workflow); // Create workflow root workflow.addAttribute("xmlns", "uri:oozie:workflow:0.4"); // <workflow-app name='xxx'></workflow-app> if (jobname == null || "".equals(jobname)) workflow.addAttribute("name", "Not specified"); else workflow.addAttribute("name", jobname); Queue<NodeDef> que = new LinkedList<NodeDef>(); que.add(start); while (!que.isEmpty()) { NodeDef cur = que.remove(); cur.append2XML(workflow); for (NodeDef toNode : cur.getOutNodes()) { toNode.delInNode(cur); if (toNode.getInDegree() == 0) que.add(toNode); } } // Set XML document format OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // Set XML encoding, use the specified encoding to save the XML document to the string, it can be specified GBK or ISO8859-1 outputFormat.setEncoding("UTF-8"); outputFormat.setSuppressDeclaration(true); // Whether generate xml header outputFormat.setIndent(true); // Whether set indentation outputFormat.setIndent(" "); // Implement indentation with four spaces outputFormat.setNewlines(true); // Set whether to wrap try { // stringWriter is used to save xml document StringWriter stringWriter = new StringWriter(); // xmlWriter is used to write XML document to string(tool) XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat); // Write the created XML document into the string xmlWriter.write(xmldoc); xmlWriter.close(); System.out.println( stringWriter.toString().trim()); // Print the string, that is, the XML document return stringWriter.toString().trim(); } catch (Exception ex) { ex.printStackTrace(); } return null; }
Example 18
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 19
Source File: XmlHelper.java From atlas with Apache License 2.0 | 3 votes |
public static void saveDocument(Document document, File file) throws IOException { file.getParentFile().mkdirs(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); saveFile(document, format, file); }
Example 20
Source File: XMLUtil.java From APICloud-Studio with GNU General Public License v3.0 | 2 votes |
/** * 写XML文件 * @param file 文件 * @param document XML对象 * @return * @throws IOException */ public static boolean saveXml(File file, Document document) throws IOException { OutputFormat format = OutputFormat.createPrettyPrint(); // 紧缩 format.setEncoding("UTF-8"); // 设置UTF-8编码 return saveXml(file, document, format); }