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<String,Object> result = getMap(element);
int level = 0;
printMap(result,level);
}
private static void printMap(Map<String,Object> 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<String,Object> getMap(Node node) {
Map<String,Object> 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 version="1.0" encoding="UTF-8"?>
<employees bb="bbbbb" yy="yyyyy">
<employee1 id="11" name="xiaowang">
<sex>man</sex>
<age>25</age>
</employee1>
<employee2 id="12" name="liyi">
<sex>woman</sex>
<age>45</age>
</employee2>
</employees>
打印结果:
key: bb value: bbbbb
key: yy value: yyyyy
key: employee1
key: sex value: man
key: name value: xiaowang
key: id value: 11
key: age value: 25
key: employee2
key: sex value: woman
key: name value: liyi
key: id value: 12
key: age value: 45
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。