博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
循环解析xml dom
阅读量:4298 次
发布时间:2019-05-27

本文共 3479 字,大约阅读时间需要 11 分钟。

最近有用到,简单写一下。不感知内容,递归调用。

package com.ferraborghini.parser;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class XmlParser {    public static void main(String[] args) throws SAXException, IOException,            ParserConfigurationException {        File xml = new File("conf/test.xml");        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        DocumentBuilder builder = factory.newDocumentBuilder();        Document doc = builder.parse(xml);        Element element = doc.getDocumentElement();        Map
result = getMap(element); int level = 0; printMap(result, level); } private static void printMap(Map
result, int level) { for (String key : result.keySet()) { if (result.get(key) instanceof Map) { System.out.println("key: "+ key); printMap((Map)result.get(key), ++level); level--; } else { for (int i = 0; i < level*5; i++) {
System.out.print(" "); } System.out.println("key: "+ key + " value: "+ result.get(key)); } } } public static Map
getMap(Node node) { Map
domMap = new HashMap(); if (node.hasAttributes()) { NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) {
domMap.put(attrs.item(i).getNodeName(), attrs.item(i) .getNodeValue());// System.out.println("attr name: " + attrs.item(i).getNodeName()); } } NodeList nodelist = node.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) {
Node child = nodelist.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { String result = getChild(child); if (result == null) { domMap.put(child.getNodeName(), getMap(child)); } else { domMap.put(child.getNodeName(), result); } } } return domMap; } /** * 如果下一个节点是text node获取内容 * @param node * @return */ public static String getChild(Node node) { String result = null; NodeList nodelist = node.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) {
if (nodelist.item(i).getNodeType() == Node.TEXT_NODE && (nodelist.item(i).getTextContent().matches("\\S+"))) { result = nodelist.item(i).getTextContent(); } } return result; }}

网上随便找了一个xml文件:

man
25
woman
45

打印结果:

key: bb value: bbbbbkey: yy value: yyyyykey: employee1     key: sex value: man     key: name value: xiaowang     key: id value: 11     key: age value: 25key: employee2     key: sex value: woman     key: name value: liyi     key: id value: 12     key: age value: 45

转载地址:http://apnws.baihongyu.com/

你可能感兴趣的文章
3、JavaWeb学习之基础篇—JSP
查看>>
4、JavaWeb学习之基础篇—Session
查看>>
5、JavaWeb学习之基础篇—标签(自定义&JSTL)
查看>>
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>
suse创建的虚拟机,修改ip地址
查看>>