Read an XML File using DOM Parser in Java
LokeshGuptaIndiaAlexGussinRussiaDavidFeezorUSA
* XmlDomParser.java
package org.example;import org.w3c.dom.Document;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import java.io.File;import java.io.IOException;/* @ref:https://howtodoinjava.com/java/xml/read-xml-dom-parser-example/ */public class XmlDomParser {public static Document readXMLDocumentFromFile(String fileNameWithPath)throws ParserConfigurationException, IOException, SAXException {//Get Document BuilderDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();//Build DocumentDocument document = builder.parse(new File(fileNameWithPath));//Normalize the XML Structure; It's just too important !!document.getDocumentElement().normalize();return document;}public static void main(String[] args) {try {Document document = readXMLDocumentFromFile("employees.xml");//Verify XML Content//Here comes the root nodeElement root = document.getDocumentElement();System.out.println(root.getNodeName());//Get all employeesNodeList nList = document.getElementsByTagName("employee");System.out.println("============================");for (int temp = 0; temp < nList.getLength(); temp++) {Node node = nList.item(temp);if (node.getNodeType() == Node.ELEMENT_NODE) {//Print each employee's detailElement eElement = (Element) node;System.out.println("\nEmployee id : " + eElement.getAttribute("id"));System.out.println("First Name : " + eElement.getElementsByTagName("firstName").item(0).getTextContent());System.out.println("Last Name : " + eElement.getElementsByTagName("lastName").item(0).getTextContent());System.out.println("Location : " + eElement.getElementsByTagName("location").item(0).getTextContent());}}} catch (Exception e) {e.printStackTrace();}}}
C:\Users\admin\.jdks\corretto-1.8.0_352\bin\java.exe “-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2022.2.3\lib\idea_rt.jar=60391:E:\Program Files\JetBrains\IntelliJ IDEA 2022.2.3\bin” -Dfile.encoding=UTF-8 -classpath C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\charsets.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\access-bridge-64.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\cldrdata.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\dnsns.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\jaccess.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\jfxrt.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\localedata.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\nashorn.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\sunec.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\sunjce_provider.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\sunmscapi.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\sunpkcs11.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\ext\zipfs.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\jce.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\jfr.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\jfxswt.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\jsse.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\management-agent.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\resources.jar;C:\Users\admin\.jdks\corretto-1.8.0_352\jre\lib\rt.jar;M:\Projects\XmlDomParse\target\classes org.example.XmlDomParser
============================
Employee id : 111
First Name : Lokesh
Last Name : Gupta
Location : India
Employee id : 222
First Name : Alex
Last Name : Gussin
Location : Russia
Employee id : 333
First Name : David
Last Name : Feezor
Location : USA
Process finished with exit code 0
jdk1.8自带的包,不需要maven引入第三方包。