1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 生成干部任免审批表

生成干部任免审批表

时间:2019-10-18 05:31:45

相关推荐

生成干部任免审批表

干部任免审批表:说白了就是国有企业独有的一种格式,省了手抄信息

1.具体就是这个样子:

![在这里插入图片描述](https://img-/8ea24364728544228c1148746bea46c3.png

2.步骤就是:

拿到该人员信息,生成一个类似xml格式的文件,文件后缀名以.lrmx结束,再用专用的编辑器打开就可以了。

3.代码难点:

①取人员信息,需要很多表关联,是一个重复写关联表的过程

②下面生成xml格式文件,写前端下载。OK

3.代码示例:

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import mons.io.FileUtils;

import mons.io.IOUtils;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

import org.xml.sax.SAXException;

import java.io.File;

import java.io.IOException;

import java.io.StringReader;

import java.io.StringWriter;

import .URL;

import java.nio.file.Paths;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class JsonToXml {

private static final String ENCODING = “UTF-8”;

/*** Json to xml string.** @param json the json* @return the string*/public static String jsonToXml(String json){try {StringBuffer buffer = new StringBuffer();buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?> \n <Person> \n");JSONObject jObj = JSONObject.fromObject(json);jsonToXmlstr(jObj,buffer);buffer.append("<Version>3.2.1.16</Version> \n");buffer.append("</Person>");return buffer.toString();} catch (Exception e) {e.printStackTrace();return "";}}/*** Json to xmlstr string.** @param jObj the j obj* @param buffer the buffer* @return the string*/public static String jsonToXmlstr(JSONObject jObj,StringBuffer buffer ){Set<Map.Entry<String, Object>> se = jObj.entrySet();for(Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); ){Map.Entry<String, Object> en = it.next();if(en.getValue().getClass().getName().equals("net.sf.json.JSONObject")){buffer.append("<"+en.getKey()+">");JSONObject jo = jObj.getJSONObject(en.getKey());jsonToXmlstr(jo,buffer);buffer.append("</"+en.getKey()+"> \n");}else if(en.getValue().getClass().getName().equals("net.sf.json.JSONArray")){JSONArray jarray = jObj.getJSONArray(en.getKey());for (int i = 0; i < jarray.size(); i++) {if(i==0) {buffer.append("<" + en.getKey() + ">");}buffer.append("<"+"Item"+">");JSONObject jsonobject = jarray.getJSONObject(i);jsonToXmlstr(jsonobject,buffer);buffer.append("</"+"Item"+">");if(i<jarray.size()-1){continue;}buffer.append("</"+en.getKey()+"> \n");}}else if(en.getValue().getClass().getName().equals("java.lang.String")){buffer.append("<"+en.getKey()+">"+en.getValue());buffer.append("</"+en.getKey()+"> \n");}}return buffer.toString();}

//

// /**

// * JSON对象转的xml字符串-格式化 带空格 换行符 输出到文件可读性好

// *

// * @param json JSON对象

// * @return 漂亮的xml字符串

///

// public static String jsonToFormatXml(JSONObject json) {

// StringWriter formatXml = new StringWriter();

// try {

// Document document = jsonToDocument(json);

// /格式化xml/

// OutputFormat format = OutputFormat.createPrettyPrint();

// // 设置缩进为4个空格

// format.setIndent(" ");

// format.setIndentSize(4);

// XMLWriter writer = new XMLWriter(formatXml, format);

// writer.write(document);

// } catch (IOException e) {

// e.printStackTrace();

// }

// return formatXml.toString();

// }

//

// /*

// * JSON对象转xml字符串-不带空格换行符

// *

// * @param json JSON对象

// * @return xml字符串

// * @throws SAXException

///

// public static String JsonToXml(JSONObject json) {

// return jsonToDocument(json).asXML();

// }

//

/*

* JSON对象转Document对象

*

* @param json JSON对象

* @return Document对象

/

public static Document jsonToDocument(JSONObject json) {

Document document = DocumentHelper.createDocument();

document.setXMLEncoding(ENCODING);

for (String rootKey : json.keySet()) {

Element root = DocumentHelper.createElement(rootKey);

// JSONObject cc = new JSONObject(rootKey);

JSONObject rootkeyjson = json.parseObject(rootKey);

addJsonObjectElement(json.getJSONObject(rootKey), root);

document.add(root);

// root对象只能有一个,多个时只读一个

break;

}

return document;

}

//

// /*

// * 添加JSONArray Element

// *

// * @param jsonArray 传入的json数组

// * @param nodeName 节点名称

// * @param arrRoot 上层节点

///

// private static void addJsonArrayElement(JSONArray jsonArray, String nodeName, Element arrRoot) {

//

// for (Object aJsonArray : jsonArray) {

// JSONObject jsonObject = (JSONObject) aJsonArray;

// Element node = DocumentHelper.createElement(nodeName);

// // 继续遍历

// for (String key : jsonObject.keySet()) {

// Element element = DocumentHelper.createElement(key);

// Object child = JSON.toJSON(jsonObject.get(key));

// if (child instanceof JSONArray) {

// // 递归

// addJsonArrayElement(jsonObject.getJSONArray(key), key, node);

// } else if (child instanceof JSONObject) {

// addJsonObjectElement(jsonObject.getJSONObject(key), element);

// node.add(element);

// } else {

// element.setText(jsonObject.getString(key));

// node.add(element);

// }

// }

// arrRoot.add(node);

// }

//

// }

//

//

// /*

// * 添加JSONObject Element

// *

// * @param json JSON对象

// * @param root 上层节点

// * @return Element对象

// */

// private static void addJsonObjectElement(JSONObject json, Element root) {

//

// for (String key : json.keySet()) {

// Element node = DocumentHelper.createElement(key);

// Object child = json.get(key);

// if (child instanceof JSONObject) {

// addJsonObjectElement(json.getJSONObject(key), node);

// root.add(node);

// } else if (child instanceof JSONArray) {

// addJsonArrayElement(json.getJSONArray(key), key, root);

// } else {

// node.setText(json.getString(key));

// root.add(node);

// }

// }

//

// }

/*** XML字符串转JSON对象** @param xml xml字符串* @return JSON对象* @throws DocumentException*/public static JSONObject xmlToJson(String xml) throws DocumentException {JSONObject json = new JSONObject();SAXReader reader = new SAXReader();Document document = reader.read(new StringReader(xml));Element root = document.getRootElement();json.put(root.getName(), elementToJson(root));return json;}/*** Element对象转JSON对象** @param element Element对象* @return JSON对象*/public static JSONObject elementToJson(Element element) {JSONObject json = new JSONObject();for (Object child : element.elements()) {Element e = (Element) child;if (e.elements().isEmpty()) {json.put(e.getName(), e.getText());} else {json.put(e.getName(), elementToJson(e));}}return json;}/*** 字符串输出到文件** @param str字符串内容* @param filePath 文件路径* @throws IOException*/public static void stringToFile(String str, String filePath) throws IOException {FileUtils.writeStringToFile(Paths.get(filePath).toFile(), str, ENCODING);}/*** 字符串输出到文件** @param str字符串内容* @param filePath 文件路径* @throws IOException*/public static void stringToFile(String str, URL filePath) throws IOException {FileUtils.writeStringToFile(new File(filePath.getPath()), str, ENCODING);}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。