Java读取XML

Read an XML File using DOM Parser in Java

html 复制代码
<employees>
    <employee id="111">
        <firstName>Lokesh</firstName>
        <lastName>Gupta</lastName>
        <location>India</location>
    </employee>
    <employee id="222">
        <firstName>Alex</firstName>
        <lastName>Gussin</lastName>
        <location>Russia</location>
    </employee>
    <employee id="333">
        <firstName>David</firstName>
        <lastName>Feezor</lastName>
        <location>USA</location>
    </employee>
</employees>

* XmlDomParser.java

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 Builder
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        //Build Document
        Document 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 node
            Element root = document.getDocumentElement();
            System.out.println(root.getNodeName());

            //Get all employees
            NodeList 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 detail
                    Element 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引入第三方包。

相关推荐
UI前端开发工作室25 分钟前
数字孪生技术为UI前端提供新视角:产品性能的实时模拟与预测
大数据·前端
Sapphire~28 分钟前
重学前端004 --- html 表单
前端·html
今天背单词了吗98030 分钟前
算法学习笔记:17.蒙特卡洛算法 ——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·笔记·考研·算法·蒙特卡洛算法
Dcs34 分钟前
从 C 到 Rust:一位开发者的 `tmux` 全面移植之旅
java
Maybyy1 小时前
力扣242.有效的字母异位词
java·javascript·leetcode
遇到困难睡大觉哈哈1 小时前
CSS中的Element语法
前端·css
Real_man1 小时前
新物种与新法则:AI重塑开发与产品未来
前端·后端·面试
小彭努力中1 小时前
147.在 Vue3 中使用 OpenLayers 地图上 ECharts 模拟飞机循环飞行
前端·javascript·vue.js·ecmascript·echarts
老马聊技术1 小时前
日历插件-FullCalendar的详细使用
前端·javascript
咔咔一顿操作1 小时前
Cesium实战:交互式多边形绘制与编辑功能完全指南(最终修复版)
前端·javascript·3d·vue