本文共 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(); Mapresult = 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/