节点类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class NodeStructure {
/**
* 节点id
*/
int id;
/**
* 父节点id
*/
int pid;
/**
* 节点名
*/
String selfname;
/**
* 节点文本
*/
String text;
}
解析工具
接口类XmlParse
import com.govthai.datamigrate.pojo.NodeStructure;
import java.util.List;
import java.util.Map;
public interface XmlParse {
Map<String,String> getValues(List<NodeStructure> ln, String... params) ;
/**
*
* @param src xml文件路径
* @return 节点List
*/
List<NodeStructure> parseXmlToList(String src);
//暂时只提供一个值的查询,不支持list
String getValue(List<NodeStructure> ln,String selfname,String sameLevelName,String value) ;
/**
* 获取某一节点下的Map集合
* @param ln 节点list
* @param parentname 父节点名称
* @return Map
*/
Map<Integer,Map> getMap(List<NodeStructure> ln,String parentname);
}
实现类XmlParseImpl
import com.govthai.datamigrate.pojo.NodeStructure;
import com.govthai.datamigrate.utils.XmlParse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Component
public class XmlParseImpl implements XmlParse {
public static int times = 0;
public static Map<Object, Integer> countMap = new HashMap();
/**
* 解析Xml字符串成
* @param src
* @return
* @throws DocumentException
*/
public static List<NodeStructure> parseXml(String src) throws DocumentException {
times++;
HashMap map = new HashMap();
List<NodeStructure> list = new LinkedList();
countMap.put(times, 0);
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File(src));
Element rootNode = doc.getRootElement();
NodeStructure nodeStructure = new NodeStructure(0, -1, rootNode.getName(), null);
list.add(nodeStructure);
//遍历根节点以下并进行存储
list = XmlParseImpl.allNodes(list, rootNode, 0, 0, times);
return list;
}
/**
* 给指定节点添加element
* @param list
* @param element
* @param i
* @param pid
* @param times
* @return
*/
private static List<NodeStructure> allNodes(List<NodeStructure> list, Element element, int i, int pid, int times) {
List<Element> elements = element.elements();
for (Element element1 : elements) {
NodeStructure nodeStructure = new NodeStructure();
nodeStructure.setId(countMap.get(times) + 1);
countMap.put(times, countMap.get(times) + 1);
nodeStructure.setPid(pid);
nodeStructure.setSelfname(element1.getName());
nodeStructure.setText(element1.getText().trim());
list.add(nodeStructure);
list = XmlParseImpl.allNodes(list, element1, i, nodeStructure.getId(), times);
}
return list;
}
//暂时不支持一个xml文件允许多个同样标签存在,因此留下拓展的接口
public static List<String> getValues(List<NodeStructure> ln, String nodename) {
return null;
}
//暂时不支持一个xml文件允许多个同样标签存在,因此留下拓展的接口(查询的是标签下一个标签有多少个值)
@Override
public Map<String, String> getValues(List<NodeStructure> ln, String... params) {
String nodename = params[0];
Map<String, String> map = new HashMap<>();
for (NodeStructure nodeStructure : ln) {
for (String param : params) {
if (nodeStructure.getSelfname().equals(param)) {
if (map.get(param) == null) {
map.put(param, nodeStructure.getText().trim());
} else {
map.put(param, map.get(param) + "," + nodeStructure.getText());
}
}
}
}
return map;
}
@Override
public List<NodeStructure> parseXmlToList(String src) {
List<NodeStructure> ln = new LinkedList<NodeStructure>();
try {
ln = XmlParseImpl.parseXml(src);
} catch (Exception e) {
System.out.println("解析xml出错");
e.printStackTrace();
}
return ln;
}
@Override
public String getValue(List<NodeStructure> ln, String selfname, String sameLevelName, String value) {
List<NodeStructure> templn = new LinkedList<NodeStructure>();
for (NodeStructure nodeStructure : ln) {
if (nodeStructure.getSelfname().equals(selfname)) {
templn.add(nodeStructure);
}
if (nodeStructure.getSelfname().equals(sameLevelName)) {
templn.add(nodeStructure);
}
}
int pid = 0;
for (int i = 0; i < templn.size(); i++) {
if (templn.get(i).getSelfname().equals(sameLevelName) && templn.get(i).getText().equals(value)) {
pid = templn.get(i).getPid();
break;
}
}
String selfValue = null;
for (NodeStructure nodeStructure : templn) {
if (nodeStructure.getPid() == pid) {
if (nodeStructure.getSelfname().equals(selfname)) {
selfValue = nodeStructure.getText();
break;
}
}
}
return selfValue;
}
//集合解析
@Override
public Map<Integer, Map> getMap(List<NodeStructure> ln, String parentname) {
//得到父级所有id
LinkedList<Integer> pids = new LinkedList<>();
Map<Integer, Map> maps = new HashMap<>();
for (NodeStructure nodeStructure : ln) {
if (parentname.equals(nodeStructure.getSelfname())) {
pids.add(nodeStructure.getId());
}
}
for (Integer pid : pids) {
for (NodeStructure nodeStructure : ln) {
if (nodeStructure.getPid() == pid) {
Map map = maps.get(pid);
if (map == null) {
HashMap map1 = new HashMap();
map1.put(nodeStructure.getSelfname(), nodeStructure.getText().trim());
maps.put(pid, map1);
} else {
Map map1 = maps.get(pid);
map1.put(nodeStructure.getSelfname(), nodeStructure.getText().trim());
maps.put(pid, map1);
}
}
}
}
return maps;
}
}