<?xml version="1.0" encoding="utf-8" ?>
<class>
<info>
<key>jack</key>
<value>345</value>
</info>
<info>
<key>Bob</key>
<value>567</value>
</info>
</class>
package com.dxy.study_0628.study.xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.*;
public class DOMReadDemo {
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("D:\\springmvc\\study_0628\\src\\main\\resources\\xml\\teacher.xml");
NodeList booklist = document.getElementsByTagName("info");
for (int i = 0; i < booklist.getLength(); i++) {
System.out.println("--------第" + (i + 1) + "本书----------");
Element ele = (Element) booklist.item(i);
NodeList childNodes = ele.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node n = childNodes.item(j);
if (n.getNodeName() != "#text") {
System.out.println(n.getNodeName() + ":" + n.getTextContent());
}
}
System.out.println("---------------------------------");
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
public void Xml(String path, List<Map<String, String>> lists) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(path);
NodeList booklist = document.getElementsByTagName("info");
for (int i = 0; i < booklist.getLength(); i++) {
HashMap<String, String> map = new HashMap<>();
System.out.println("--------第" + (i + 1) + "本书----------");
Element ele = (Element) booklist.item(i);
NodeList childNodes = ele.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node n = childNodes.item(j);
if (n.getNodeName() != "#text") {
System.out.println(n.getNodeName() + ":" + n.getTextContent());
map.put(n.getNodeName(), n.getTextContent());
lists.add(map);
}
}
System.out.println("---------------------------------");
}
}
public List<Map<String, String>> Xml_TF() throws IOException, SAXException, ParserConfigurationException {
String path="";
List<Map<String, String>> lists = new ArrayList<>();
ResourceBundle resource = ResourceBundle.getBundle("xml/myconfig");
String key = resource.getString("flag");
if ("dev".equals(key)){
path="src/main/resources/xml/student.xml";
}else {
path="src/main/resources/xml/teacher.xml";
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(path);
NodeList booklist = document.getElementsByTagName("info");
for (int i = 0; i < booklist.getLength(); i++) {
HashMap<String, String> map = new HashMap<>();
Element ele = (Element) booklist.item(i);
NodeList childNodes = ele.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node n = childNodes.item(j);
if (n.getNodeName() != "#text") {
System.out.println(n.getNodeName() + ":" + n.getTextContent());
map.put(n.getNodeName(), n.getTextContent());
lists.add(map);
}
}
}
LinkedHashSet<Map<String, String>> hashSet = new LinkedHashSet<>(lists);
ArrayList<Map<String, String>> list = new ArrayList<>(hashSet);
return list;
}
}
https://www.jb51.net/article/115316.htm