All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.gitee.huanminabc.utils_common.base.XmlGetDom4AndXPathValueUtil Maven / Gradle / Ivy
package com.gitee.huanminabc.utils_common.base;
import com.gitee.huanminabc.utils_common.obj.reflect.FieldUtil;
import org.dom4j.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//https://www.w3school.com.cn/xpath/xpath_axes.asp
// 获取xml节点和属性的value
public class XmlGetDom4AndXPathValueUtil {
// 将xml字符串转换为Document对象
public static Document parseXmlString(String xmlString) {
Document document = null;
try {
document = DocumentHelper.parseText(xmlString);
} catch (DocumentException e) {
throw new RuntimeException(e);
}
return document;
}
//将Document对象转换为xml字符串
public static String parseXmlString(Document document) {
return document.asXML();
}
//将xml字符串转换为Map对象 ,只支持一层
public static Map getXmlToMap(String xml,String rootName ,String tabName,String attrName) {
Map map = new HashMap<>();
Document document = parseXmlString(xml);
Node root = document.selectSingleNode("//" + rootName);
List nodes = root.selectNodes("//"+tabName);
for (Node node : nodes) {
Node arrtN = node.selectSingleNode("@" + attrName);
String arrtV = arrtN.getText();
map.put(arrtV,node.getText());
}
return map;
}
//将xml字符串转换为List对象 ,平级支持多层
public static List> getXmlToMaps(String xml,String rootNames ,String tabName,String attrName) {
List> list = new ArrayList<>();
Document document = parseXmlString(xml);
List roots = document.selectNodes("//" + rootNames);
for (Node root : roots) {
Map map = new HashMap<>();
List nodes = root.selectNodes("child::"+tabName);
for (Node node : nodes) {
Node arrtN = node.selectSingleNode("@" + attrName);
String arrtV = arrtN.getText();
map.put(arrtV,node.getText());
}
list.add(map);
}
return list;
}
//将Map对象转换为xml字符串
public static String getMapToXml(Map map, String rootName, String tabName, String attrName) {
Document document = DocumentHelper.createDocument();
Element root = document.addElement(rootName);
for (Map.Entry entry : map.entrySet()) {
Element tab = root.addElement(tabName);
tab.addAttribute(attrName, entry.getKey());
tab.setText(entry.getValue());
}
return parseXmlString(document);
}
//将bean对象转换为xml字符串
public static String getBeanToXml(Object bean, String rootName, String tabName, String attrName) {
Document document = DocumentHelper.createDocument();
try {
Class> aClass = bean.getClass();
Element root = document.addElement(rootName);
Field[] fields = FieldUtil.getFields(aClass);//获取所有属性
for (Field field : fields) {
Element tab = root.addElement(tabName);
tab.addAttribute(attrName, field.getName());
Object v = field.get(bean);
tab.setText(v == null ? "" : v.toString());
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return parseXmlString(document);
}
//将xml字符串转换为bean对象 ,只支持一层,并且属性名和xml属性值一致,Bean类型必须都是String 否则报错 java.lang.IllegalArgumentException: Can not set int field
public static T getXmlToBean(String xml, String rootName, String tabName, String attrName, Class clazz) {
T t = null;
try {
t = clazz.newInstance();
Document document = parseXmlString(xml);
Node root = document.selectSingleNode("//" + rootName);
List nodes = root.selectNodes("//"+tabName);
for (Node node : nodes) {
Node arrtN = node.selectSingleNode("@" + attrName);
String arrtV = arrtN.getText();
Field field = FieldUtil.getField(clazz, arrtV);
if (field != null) {
field.setAccessible(true);
field.set(t, node.getText());
}
}
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return t;
}
// 获取xml指定tabName 的第一个值
public static String getXmlByTabNameOne(String xmlString, String tabName) {
Document document = parseXmlString(xmlString);
Node node = document.selectSingleNode("//" + tabName);
return node == null ? null : node.getText();
}
// 获取xml指定tabName 的所有值
public static List getXmlByTabName(String xmlString, String tabName) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//" + tabName);
List list = new ArrayList<>();
for (Node node : nodes) {
list.add(node.getText());
}
return list;
}
// 获取xml指定attrName的第一个值
public static String getXmlByAttrNameOne(String xmlString, String attrName) {
Document document = parseXmlString(xmlString);
Node node = document.selectSingleNode("//@" + attrName);
return node == null ? null : node.getText();
}
// 获取xml指定attrName的所有值
public static List getXmlByAttrName(String xmlString, String attrName) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//@" + attrName);
List list = new ArrayList<>();
for (Node node : nodes) {
list.add(node.getText());
}
return list;
}
// 获取xml指定tabName , attrName=attrValue
public static String getXmlByTabNameAndAttrNameEqAttrValueOne(String xmlString, String tabName, String attrName, String attrValue) {
Document document = parseXmlString(xmlString);
Node node = document.selectSingleNode("//" + tabName + "[@" + attrName + "='" + attrValue + "']");
return node == null ? null : node.getText();
}
// 获取xml指定tabName , attrName=attrValue
public static List getXmlByTabNameAndAttrNameEqAttrValueAll(String xmlString, String tabName, String attrName, String attrValue) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//" + tabName + "[@" + attrName + "='" + attrValue + "']");
List list = new ArrayList<>();
for (Node node : nodes) {
list.add(node.getText());
}
return list;
}
//获取指定tabName包含attrName的第一个tabName值
public static List getXmlByTabNameAndAttrNameRetTabNameOne(String xmlString, String tabName, String attrName) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//" + tabName + "[@" + attrName + "]");
List list = new ArrayList<>();
for (Node node : nodes) {
list.add(node.getText());
}
return list;
}
//获取指定tabName包含attrName的所有tabName值
public static List getXmlByTabNameAndAttrNameRetTabNameAll(String xmlString, String tabName, String attrName) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//" + tabName + "[@" + attrName + "]");
List list = new ArrayList<>();
for (Node node : nodes) {
list.add(node.getText());
}
return list;
}
//获取指定tabName包含attrName的第一个attrName值
public static List getXmlByTabNameAndAttrNameRetAttrValueOne(String xmlString, String tabName, String attrName) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//" + tabName + "[@" + attrName + "]");
List list = new ArrayList<>();
for (Node node : nodes) {
Node arrtN = node.selectSingleNode("@" + attrName);
list.add(arrtN.getText());
}
return list;
}
//获取指定tabName包含attrName的所有attrName值
public static List getXmlByTabNameAndAttrNameRetAttrValueAll(String xmlString, String tabName, String attrName) {
Document document = parseXmlString(xmlString);
List nodes = document.selectNodes("//" + tabName + "[@" + attrName + "]");
List list = new ArrayList<>();
for (Node node : nodes) {
Node arrtN = node.selectSingleNode("@" + attrName);
list.add(arrtN.getText());
}
return list;
}
}