

新闻资讯
技术学院Java中将XML转为嵌套Map的核心是用DOM解析器递归遍历节点,以元素名为key、文本或子Map为value,同名兄弟节点存为List,混合内容用#text键分离,忽略空白文本节点。
Java中把XML转换成嵌套的Map结构,核心思路是解析XML树,递归遍历节点,将元素名作为key、文本内容或子节点Map作为value,构建出类似JSON对象的嵌套Map(如 Map)。推荐使用轻量、易控的DOM解析器,配合递归逻辑实现,避免引入复杂框架(如Jackson XML)带来的类型约束或配置负担。
DOM适合小到中等XML(内存可接受),能直观反映层级关系。关键点:区分元素节点与文本节点,处理同名兄弟节点(转为List),忽略空白文本节点。
DocumentBuilder 加载XML字符串或文件,获取根 Element
nodeToMap(Node node),对每个元素节点:
Element 节点,按标签名分组(同名多个 → 放入 List)getTextContent().trim()非空),且无子元素,则该元素值为字符串;否则值为子Map或Map列表@attributes 子Map(可选,按需启用)真实XML常有重复标签(如 )或元素内含文本+子元素(混合内容)。这时不能简单用 Map 直接put,需判断:
ArrayList,而非单个MapJohnJr. ),建议将文本存为 #text key,子元素照常嵌套(类似JavaScript DOM风格)Node.TEXT_NODE 中纯空白(isAllWhitespace()),防止空格污染Map
以下代码片段可直接复用(已处理常见边界):
public static MapxmlToMap(String xml) throws Exception { Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())); return nodeToMap(doc.getDocumentElement()); } private static Map nodeToMap(Node node) { Map result = new LinkedHashMap<>(); // 处理属性 if (node.getAttributes() != null) { Map attrs = new LinkedHashMap<>(); for (int i = 0; i < node.getAttributes().getLength(); i++) { Node attr = node.getAttributes().item(i); attrs.put(attr.getNodeName(), attr.getNodeValue()); } if (!attrs.isEmpty()) result.put("@attributes", attrs); } NodeList children = node.getChildNodes(); Map >> childGroups = new LinkedHashMap<>(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { String name = child.getNodeName(); childGroups.computeIfAbsent(name, k -> new ArrayList<>()) .add(nodeToMap(child)); } else if (child.getNodeType() == Node.TEXT_NODE) { String text = child.getTextContent().trim(); if (!text.isEmpty()) { result.put("#text", text); } } } // 拆分group:单个→Map,多个→List
基本上就这些。不复杂但容易忽略空白文本和同名节点合并逻辑。如果XML较大或需流式处理,可换用StAX(XMLStreamReader)手动构建Map,控制内存更精细。