package com.example.demo_java8_new_characteristic.test;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JdomUtils {
public static Document loadXmlByString(String xml) throws JDOMException, IOException {
StringReader in = new StringReader(xml);
SAXBuilder sb = new SAXBuilder();
return sb.build(in);
}
public static Document loadXmlByFile(File f) throws IOException, JDOMException {
BufferedReader reader = new BufferedReader(new FileReader(f));
String buff = "";
String str = "";
while ((buff = reader.readLine()) != null){
str += buff;
}
return loadXmlByString(str);
}
/**
* 获取节点值,如果节点不存在,返回空
* @param path
* @param doc
* @return
* @throws Exception
*/
public static String getNodeValue(String path,Document doc) throws Exception {
String ret = "";
path = paraInit(path, doc);
String[] paths = path.split("/");
int len = paths.length;
if(len == 1){
throw new Exception("path is invalid");
}else {
Element root = doc.getRootElement();
Element e = root;
for (int i =1;i<len;i++){
e = e.getChild(paths[i]);
if (e==null)
return "";
}
ret = e.getValue();
}
return ret == null? "": ret;
}
public static String getNodeText(String path,Document doc) throws Exception {
String ret = "";
path = paraInit(path, doc);
String[] paths = path.split("/");
int len = paths.length;
if (len == 1){
throw new Exception("path is invalid");
}else {
Element root = doc.getRootElement();
Element e = root;
for (int i=1;i<len;i++){
e = e.getChild(paths[i]);
}
ret = e.getText();
}
return ret == null? "":ret;
}
public static Map getNodeListAndMap(String listPath,String listAttrKey,String listAttrValue,String mapAttrKey,String mapAttrValue,Document doc) throws Exception {
listPath = paraInit(listPath, doc);
String[] paths = listPath.split("/");
int len = paths.length;
Map retMap = new HashMap<>();
if (len == 1){
throw new Exception("path is invalid");
}else {
Element root = doc.getRootElement();
Element e = root;
for (int i =1;i<len;i++){
e = e.getChild(paths[i]);
}
List l = e.getChildren();
for (int i =0;i<l.size();i++){
Element ee = (Element)l.get(i);
String attr1 = ee.getAttributeValue(listAttrKey);
String attr2 = ee.getAttributeValue(listAttrValue);
Map map1 = new HashMap<>();
map1.put(attr1,attr2);
Map map2 = new HashMap<>();
List l2 = ee.getChildren();
for (int ii = 0; ii <l2.size();ii++){
Element eee = (Element) l2.get(i);
String retAttr1 = eee.getAttributeValue(mapAttrKey);
String retAttr2 = eee.getAttributeValue(mapAttrValue);
map2.put(retAttr1,retAttr2);
}
retMap.put(map1,map2);
}
}
return retMap;
}
public static List getNodeAttrList(String path,String attr,Document doc) throws Exception {
path = paraInit(path,doc);
String[] paths = path.split("/");
int len = paths.length;
List retList = new ArrayList<>();
if (len==1){
throw new Exception("path is invalid");
}else {
Element root = doc.getRootElement();
Element e = root;
for (int i = 1;i<len -1 ;i++){
e = e.getChild(paths[i]);
}
List l = e.getChildren();
for (int i = 0;i<l.size();i++){
Element ee=(Element)l.get(i);
String retAttr = ee.getAttributeValue(attr);
retList.add(i,retAttr);
}
}
return retList;
}
/**
* 不支持多个节点重复,会覆盖之前的节点
* @param path
* @param value
* @param doc
* @throws Exception
*/
public static void setNodeValue(String path,String value,Document doc) throws Exception {
path = paraInit(path, doc);
String[] paths = path.split("/");
int len = paths.length;
Element root = doc.getRootElement();
Element e = root;
for (int i=1;i<len;i++){
Element child = e.getChild(paths[i]);
if (child==null){
//节点不存在
Element tmpe = new Element(paths[i]);
e.addContent(tmpe);
e = e.getChild(paths[i]);
}else {
e=child;
}
}
e.setText(value);
}
/**
* 显示doc内容
* @param doc
* @param encode 编码格式
* @return
* @throws IOException
*/
public static String doc2String(Document doc,String encode) throws IOException {
Format format = Format.getPrettyFormat();
format.setEncoding(encode);
format.setExpandEmptyElements(true);
XMLOutputter xmlOut = new XMLOutputter(format);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlOut.output(doc,bo);
return bo.toString();
}
public static String paraInit(String path,Document doc) throws Exception {
if (doc == null){
throw new Exception("document is null");
}
if (path.startsWith("/"))
path = path.substring(1,path.length());
return path;
}
public static void main(String[] args) throws Exception {
Document doc = JdomUtils.loadXmlByString("<FACE></FACE>");
JdomUtils.setNodeValue("FACE/IN/APPNO","123",doc);
JdomUtils.setNodeValue("FACE/IN/IRANSTYPE","456",doc);
JdomUtils.setNodeValue("FACE/IN/IRANSNAME","789",doc);
JdomUtils.setNodeValue("FACE/IN/IMG1","asdfafa",doc);
JdomUtils.setNodeValue("FACE/IN/IMG2","hgsfgsdf",doc);
JdomUtils.setNodeValue("FACE/IN/NOTE1","111",doc);
JdomUtils.setNodeValue("FACE/IN/NOTE2","222",doc);
JdomUtils.setNodeValue("FACE/IN/NOTE3","333",doc);
JdomUtils.setNodeValue("FACE/IN/NOTE4","444",doc);
JdomUtils.setNodeValue("FACE/IN/NOTE5","555",doc);
JdomUtils.setNodeValue("FACE/OUT/APPNO","123",doc);
JdomUtils.setNodeValue("FACE/OUT/IRANSTYPE","456",doc);
JdomUtils.setNodeValue("FACE/OUT/IRANSNAME","789",doc);
JdomUtils.setNodeValue("FACE/OUT/IMG1","asdfafa",doc);
JdomUtils.setNodeValue("FACE/OUT/IMG2","hgsfgsdf",doc);
JdomUtils.setNodeValue("FACE/OUT/NOTE1","111",doc);
JdomUtils.setNodeValue("FACE/OUT/NOTE2","222",doc);
JdomUtils.setNodeValue("FACE/OUT/NOTE3","3333",doc);
JdomUtils.setNodeValue("FACE/OUT/NOTE4","444",doc);
JdomUtils.setNodeValue("FACE/OUT/NOTE5","555",doc);
String s = JdomUtils.doc2String(doc, "utf-8");
System.out.println(s);
}
}