实现功能
- 解析xml对应字符串,获取List 根据List的子节点 获取xml包含的全部信息
- 返回List中所有一级子节点的名称
- 递归返回List中所有节点
注意:入参字符串一定要包含节点
定义节点类
@Data
public static class Node {
private String name;
private String value;
private List<Node> children = new ArrayList<>();
private List<Node> attributes = new ArrayList<>();
private Node parent;
public Node(String name, String value) {
this.name = name;
this.value = value;
}
// 遍历获取所有子节点的名称
public List<String> getAllNames() {
List<String> allNames = new ArrayList<>();
collectAllNames(this, allNames);
return allNames;
}
private void collectAllNames(Node node, List<String> allNames) {
allNames.add(node.getName());
for (Node child : node.getChildren()) {
collectAllNames(child, allNames);
}
}
public List<Node> getChildrenByName(String name) {
List<Node> nodes = new ArrayList<>();
for (Node node : children) {
if (node.name.equals(name)) {
nodes.add(node);
}
}
return nodes;
}
@Override
public String toString() {
return "Node{" +
"name='" + name + '\'' +
", value='" + value + '\'' +
", attributes=" + attributesToString(attributes) +
", children=" + childrenToString(children) +
'}';
}
private String attributesToString(List<Node> attributes) {
if (attributes == null || attributes.isEmpty()) {
return "[]";
}
StringBuilder result = new StringBuilder("[");
for (Node attribute : attributes) {
result.append(attribute.getName()).append("=").append(attribute.getValue()).append(", ");
}
result.delete(result.length() - 2, result.length());
result.append("]");
return result.toString();
}
private String childrenToString(List<Node> children) {
if (children == null || children.isEmpty()) {
return "[]";
}
StringBuilder result = new StringBuilder("[");
for (Node child : children) {
result.append(child.toString()).append(", ");
}
result.delete(result.length() - 2, result.length());
result.append("]");
return result.toString();
}
}
工具类
package com.zhaxd.common.toolkit;
import lombok.Data;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* XMLUtils
*
* @author ZP
* @since 2023/11/10 17:30
*/
public class XmlUtils {
public static List<Node> getNodes(String xmlStr) {
List<Node> nodeList = new ArrayList<>();
if (xmlStr == null || xmlStr.length() == 0) {
return nodeList;
}
try {
Document document = parseXmlString(xmlStr);
if (document != null) {
Element root = document.getDocumentElement();
parseXmlNode(root, null, nodeList);
}
} catch (Exception e) {
e.printStackTrace();
}
return nodeList;
}
/**
* 返回List<Node>中所有一级子节点的名称
*
* @param List<Node> nodeList
* @return Set<String>
*/
public static Set<String> getChildrenNamesFromNodes(List<Node> nodeList) {
Set<String> allNames = new HashSet<>();
for (Node node : nodeList) {
allNames.addAll(node.getAllNames());
}
return allNames;
}
/**
* 返回List<Node>中所有子节点,包括子节点的子节点
*
* @param List<Node> nodeList
* @return Set<String>
*/
public static List<Node> getAllDescendantsFromNodes(List<Node> nodeList) {
List<Node> allDescendants = new ArrayList<>();
for (Node node : nodeList) {
collectAllDescendants(node, allDescendants);
}
return allDescendants;
}
private static void collectAllDescendants(Node node, List<Node> allDescendants) {
allDescendants.add(node);
for (Node child : node.getChildren()) {
collectAllDescendants(child, allDescendants);
}
}
private static Document parseXmlString(String xmlString) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlString)));
}
private static void parseXmlNode(Element element, Node parent, List<Node> nodeList) {
Node node = new Node(element.getNodeName(), element.getTextContent().trim());
node.attributes = getAttributes(element);
node.parent = parent;
nodeList.add(node);
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i) instanceof Element) {
Element childElement = (Element) children.item(i);
parseXmlNode(childElement, node, node.getChildren());
}
}
}
private static List<Node> getAttributes(Element element) {
List<Node> attributes = new ArrayList<>();
NamedNodeMap attributeNodes = element.getAttributes();
for (int i = 0; i < attributeNodes.getLength(); i++) {
org.w3c.dom.Node attributeNode = attributeNodes.item(i);
if (attributeNode instanceof Element) {
Element attributeElement = (Element) attributeNode;
Node attribute = new Node(attributeElement.getNodeName(), attributeElement.getTextContent().trim());
attributes.add(attribute);
}
}
return attributes;
}
// 定义类保存xml的节点信息
@Data
public static class Node {
private String name;
private String value;
private List<Node> children = new ArrayList<>();
private List<Node> attributes = new ArrayList<>();
private Node parent;
public Node(String name, String value) {
this.name = name;
this.value = value;
}
// 遍历获取所有子节点的名称
public List<String> getAllNames() {
List<String> allNames = new ArrayList<>();
collectAllNames(this, allNames);
return allNames;
}
private void collectAllNames(Node node, List<String> allNames) {
allNames.add(node.getName());
for (Node child : node.getChildren()) {
collectAllNames(child, allNames);
}
}
public List<Node> getChildrenByName(String name) {
List<Node> nodes = new ArrayList<>();
for (Node node : children) {
if (node.name.equals(name)) {
nodes.add(node);
}
}
return nodes;
}
@Override
public String toString() {
return "Node{" +
"name='" + name + '\'' +
", value='" + value + '\'' +
", attributes=" + attributesToString(attributes) +
", children=" + childrenToString(children) +
'}';
}
private String attributesToString(List<Node> attributes) {
if (attributes == null || attributes.isEmpty()) {
return "[]";
}
StringBuilder result = new StringBuilder("[");
for (Node attribute : attributes) {
result.append(attribute.getName()).append("=").append(attribute.getValue()).append(", ");
}
result.delete(result.length() - 2, result.length());
result.append("]");
return result.toString();
}
private String childrenToString(List<Node> children) {
if (children == null || children.isEmpty()) {
return "[]";
}
StringBuilder result = new StringBuilder("[");
for (Node child : children) {
result.append(child.toString()).append(", ");
}
result.delete(result.length() - 2, result.length());
result.append("]");
return result.toString();
}
}
}
3.使用示例
入参:
<root>
<connection>测试达梦</connection>
<schema/>
<table>TEST1</table>
<commit>1000</commit>
<truncate>N</truncate>
<ignore_errors>N</ignore_errors>
<use_batch>Y</use_batch>
<specify_fields>Y</specify_fields>
<partitioning_enabled>N</partitioning_enabled>
<partitioning_field/>
<partitioning_daily>N</partitioning_daily>
<partitioning_monthly>Y</partitioning_monthly>
<tablename_in_field>N</tablename_in_field>
<tablename_field/>
<tablename_in_table>Y</tablename_in_table>
<return_keys>N</return_keys>
<return_field/>
<fields>
<field>
<column_name>TYPE1</column_name>
<stream_name>type</stream_name>
</field>
</fields>
</root>
调用XmlUtils.getNodes返回