import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.kerberos.KerberosKey;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.context.support.StaticApplicationContext;
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 Sample1 {
public static final String AGE = "age";
public static final String SEX = "sex";
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
String str = courseFile + "/beans.xml";
System.out.println(str);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(str);
Element root = doc.getDocumentElement();
NodeList users = root.getChildNodes();
Map<String, Object> allInfo = new HashMap<String, Object>();
allInfo = getXmlAll(allInfo, users);
System.out.println(allInfo);
}
public static Map<String, Object> getXmlAll(Map<String, Object> allInfo, NodeList nodeList) {
String stuNumb = "";
if (nodeList.getLength() > 0) {
Map<String, Object> stuMap = new HashMap<String, Object>();
for (int i = 0; i < nodeList.getLength(); i++) {
Map<String, Object> stuMapPro = new HashMap<String, Object>();
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attrs = node.getAttributes();
if (attrs != null && attrs.getLength() > 0) {
Node property = attrs.getNamedItem("id");
stuNumb = property.getNodeValue();
}
switch (node.getNodeName()) {
case AGE:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(AGE, node.getFirstChild().getNodeValue());
break;
case SEX:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(SEX, node.getFirstChild().getNodeValue());
break;
default:
NodeList childList = node.getChildNodes();
System.out.println(stuNumb);
if (childList.getLength() > 0) {
Map<String, Object> stuMap2 = new HashMap<String, Object>();
stuMap2.putAll(getXmlAll(stuMap, childList));
if (stuMap2.size() > 0) {
allInfo.put(stuNumb, stuMap2);
}
}
break;
}
}
if (stuMapPro.size() > 0) {
allInfo.putAll(stuMapPro);
}
}
}
return allInfo;
}
}
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.kerberos.KerberosKey;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.context.support.StaticApplicationContext;
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 Sample1 {
public static final String AGE = "age";
public static final String SEX = "sex";
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
String str = courseFile + "/beans.xml";
System.out.println(str);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(str);
Element root = doc.getDocumentElement();
NodeList users = root.getChildNodes();
Map<String, Object> allInfo = new HashMap<String, Object>();
allInfo = getXmlAll(allInfo, users);
System.out.println(allInfo);
}
public static Map<String, Object> getXmlAll(Map<String, Object> allInfo, NodeList nodeList) {
String stuNumb = "";
if (nodeList.getLength() > 0) {
Map<String, Object> stuMap = new HashMap<String, Object>();
for (int i = 0; i < nodeList.getLength(); i++) {
Map<String, Object> stuMapPro = new HashMap<String, Object>();
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attrs = node.getAttributes();
if (attrs != null && attrs.getLength() > 0) {
Node property = attrs.getNamedItem("id");
stuNumb = property.getNodeValue();
}
switch (node.getNodeName()) {
case AGE:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(AGE, node.getFirstChild().getNodeValue());
break;
case SEX:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(SEX, node.getFirstChild().getNodeValue());
break;
default:
NodeList childList = node.getChildNodes();
System.out.println(stuNumb);
if (childList.getLength() > 0) {
Map<String, Object> stuMap2 = new HashMap<String, Object>();
stuMap2.putAll(getXmlAll(stuMap, childList));
if (stuMap2.size() > 0) {
allInfo.put(stuNumb, stuMap2);
}
}
break;
}
}
if (stuMapPro.size() > 0) {
allInfo.putAll(stuMapPro);
}
}
}
return allInfo;
}
}