/** *@Description: json数据工具 */ package com.lulei.util; import java.io.IOException; import java.util.HashMap; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonUtil { public static final String NO_DATA = "{\"data\":null}"; public static final String NO_RESULT = "{\"result\":false}"; private static ObjectMapper mapper; static{ mapper = new ObjectMapper(); //转换json时,如果对象中属性值为null,则不生成该属性 mapper.setSerializationInclusion(Include.NON_NULL); } /*** * @param json * @return 当解析失败返回null * @Author: lulei * @Description: 给定json字符串获得json对象 */ public static JsonNode josn2Object(String json){ try { return mapper.readTree(json); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /*** * @param obj * @return 当解析失败返回{datas:null} * @Author: lulei * @Description: 给定java对象生成对应json */ public static String parseJson(Object obj){ if(obj == null){ return NO_DATA; } try { return mapper.writeValueAsString(obj); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); return NO_DATA; } } /*** * @param obj * @param root * @return 当解析失败返回{datas:null} * @Author: lulei * @Description:给定java对象生成对应json,可以指定一个json的root名 */ public static String parseJson(Object obj, String root){ if(obj == null){ return NO_DATA; } try { StringBuilder sb = new StringBuilder(); sb.append("{\""); sb.append(root); sb.append("\":"); sb.append(mapper.writeValueAsString(obj)); sb.append("}"); return sb.toString(); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); return NO_DATA; } } /*** * @param json * @param var * @return 若传入var为null,则默认变量名为datas * @Author: lulei * @Description:将json字符串包装成jsonp,例如var data={}方式 */ public static String wrapperJsonp(String json, String var){ if(var == null){ var = "datas"; } return new StringBuilder().append("var ").append(var).append("=").append(json).toString(); } public static void main(String[] args) { HashMap<String, Integer> hash = new HashMap<String, Integer>(); hash.put("key1", 1); hash.put("key2", 2); hash.put("key3", 3); System.out.println(JsonUtil.parseJson(hash)); } }
/** *@Description: Xml工具类 */ package com.lulei.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Node; public class XmlUtil { private static String noResult = "<root>no result</root>"; /** * @param obj * @return * @Author:lulei * @Description: 将java对象转化为xml格式的字符串 */ public static String parseObjToXmlString(Object obj){ if (obj == null) { return noResult; } StringWriter sw = new StringWriter(); JAXBContext jAXBContext; Marshaller marshaller; try { jAXBContext = JAXBContext.newInstance(obj.getClass()); marshaller = jAXBContext.createMarshaller(); marshaller.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return noResult; } /** * @param xml * @return * @Author: lulei * @Description: 将xml String对象转化为xml对象 */ public static Document createFromString(String xml){ try { return DocumentHelper.parseText(xml); } catch (DocumentException e) { e.printStackTrace(); return null; } } /** * @param xpath * @param node * @return * @Author: lulei * @Description: 获取指定xpath的文本,当解析失败返回null */ public static String getTextFromNode(String xpath,Node node){ try { return node.selectSingleNode(xpath).getText(); } catch (Exception e) { return null; } } /** * @param path * @Author: lulei * @Description: 读取xml文件 * @return xml文件对应的Document */ public static Document createFromPath(String path){ return createFromString(readFile(path)); } /** * @param path * @Author: lulei * @Description: 读文件 * @return 返回文件内容字符串 */ private static String readFile(String path) { File file = new File(path); FileInputStream fileInputStream; StringBuffer sb = new StringBuffer(); try { fileInputStream = new FileInputStream(file); //错误使用UTF-8读取内容 String charset = CharsetUtil.getStreamCharset(file.toURI().toURL(), "utf-8"); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charset); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String s; while ((s = bufferedReader.readLine()) != null){ s = s.replaceAll("\t", "").trim(); if (s.length() > 0){ sb.append(s); } } fileInputStream.close(); bufferedReader.close(); fileInputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sb.toString(); } public static void main(String[] args) { } }
/** *@Description: 章节列表搜索结果 */ package com.lulei.test; import java.util.ArrayList; import javax.xml.bind.annotation.XmlRootElement; import com.lulei.util.XmlUtil; @XmlRootElement(name = "root") public class TestXmlUtil { private int count; private ArrayList<String> result; public TestXmlUtil() { count = 3; result = new ArrayList<String>(); result.add("test1"); result.add("test2"); result.add("test3"); } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public ArrayList<String> getResult() { return result; } public void setResult(ArrayList<String> result) { this.result = result; } public static void main(String[] args) { System.out.println(XmlUtil.parseObjToXmlString(new TestXmlUtil())); } }