dom4j: 字符串与文档互转; 创建文档、节点、属性;获取节点、属性值;
1. 创建文档Document:
Document document = DocumentHelper.createDocument();
2.创建根节点:
Element root=document.addElement("root");
3.创建子节点(并设置文本内容):
Element book=root.addElement("book")[.setText("100")];
4.添加节点属性: book.addAttribute("type","science");
5.文档转字符串:
String str = document.asXML();
6.字符串转文档:
Document document=DocumentHelper.parseText(str);
7.获取节点:
Document document = DocumentHelper.parseText(str);->
Element root=document.getRootElement(); -- 根节点
Element book=root.element("book"); -- 子节点;
8.获取节点属性值:
String type=book.attributeValue("type");
9.xpath: /DOCUMENT/BODY
List<Element> children = document.selectNodes(xpath);
fastjson: 字符串与JSON互转;JSON、JSONObject、JSONArray;
1.简单对象型: String OBJ_STR = "{\"name\":\"lily\",\"age\":12}";
2.简单对象型: String OBJ_STR = "{\"name\":\"lily\",\"age\":12}";
3. 数组类型: String ARRAY_STR = "[{\"name\":\"lily\",\"age\":12}, {\"name\":\"lucy\",\"age\":15}]";
4.复杂格式: String JSON_STR ="{\"course\":{\"name\":\"english\",\"code\":1270},
\"stu\":[{\"name\":\"lily\",\"age\":12},{\"name\":\"lucy\",\"age\":15}]}";
JSONObject jsonObject = JSON.parseObject(OBJ_STR);
JSONArray jsonArray = JSON.parseArray(JARRAY_STR);
5. JSON转字符串: String str = jsonArray.toJSONString();
6.jsonpath: $.message.body
Object result = JSONPath.eval(jsonObject, jsonpath);
XML与JSON互转
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.Iterator;
import java.util.List;
public class Json2Xml {
/**
* com.alibaba.fastjson.JSONObject 转 org.dom4j.document
*/
public static Document jsonToXml(JSONObject jsonObject) {
Document document = DocumentHelper.createDocument();
Element root;
Iterator<String> iter = jsonObject.keySet().iterator();
if (iter.hasNext()) {
String key = iter.next();
root = document.addElement(key);
jsonToElement(jsonObject, root);
}
return document;
}
/**
* @param jsonObject
* @param element
* @return
*/
public static Element jsonToElement(JSONObject jsonObject, Element element) {
Object jsonNode = jsonObject.get(element.getName());
if (jsonNode instanceof JSONArray) {
for (JSONObject childrenJson : (List<JSONObject>) jsonNode) {
addElement(childrenJson, element);
}
} else if (jsonNode instanceof JSONObject) {
JSONObject childrenJson = (JSONObject) jsonNode;
addElement(childrenJson, element);
} else {
// 递归出口
element.setText(jsonNode.toString());
}
return element;
}
/**
* 1.获取childrenJson的key值, 作为children的节点名
* 2.转换childrenJson的value,将children添加到element(递归)
*
* @param childrenJson
* @param element
* @return
*/
private static Element addElement(JSONObject childrenJson, Element element) {
Iterator<String> iter = childrenJson.keySet().iterator();
if (iter.hasNext()) {
String key = iter.next();
Element children = element.addElement(key);
jsonToElement(childrenJson, children);
}
return element;
}
/**
* org.dom4j.document 转 com.alibaba.fastjson.JSONObject
*
* @param document
* @return
*/
public JSONObject xmlToJson(Document document) {
Element root = document.getRootElement(); // 根节点
List<Element> elements = root.elements();
JSONObject jsonObject = new JSONObject();
return (JSONObject) jsonObject.put(root.getName(), this.elementToJson(elements));
}
/**
* 1.参数element集合,遍历得到element
* 2.获取element子节点集合,
* 3.子集合>0递归,否则jsonobject.put(node.name, node.text)
*
* @param elements
* @return
*/
public static JSONObject elementToJson(List<Element> elements) {
JSONObject jsonObject = new JSONObject();
for (Element element : elements) {
List<Element> children = element.elements();
if (children.size() > 0) {
jsonObject.put(element.getName(), elementToJson(children));
} else {
jsonObject.put(element.getName(), element.getText());
}
}
return jsonObject;
}
}