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引入第三方包。

相关推荐
abments9 分钟前
使用 Selenium 获取 Web 页面信息的全指南
前端·selenium·测试工具
Play_Sai12 分钟前
Vue.js 中的API接口封装实战与详解
前端·javascript·vue.js
读心悦14 分钟前
【CSS】什么是CSS的columns属性
前端·css
Wang's Blog18 分钟前
Webpack: 构建微前端应用
前端·webpack
懒人w26 分钟前
百万数据量修改数据思路及方法
java·linux·windows
软件编程工程师33 分钟前
基于vue脚手架创建的图书商城
前端·javascript·css·vue.js·html·网站·图书商城
柯基的小屁墩37 分钟前
Mac|install vue
前端·vue.js·macos
林恒smileZAZ41 分钟前
为什么 [] == ![] 为 true?
前端·javascript
常乐hhh1 小时前
浏览器内核与渲染机制深入解读
前端·性能优化·浏览器
亦世凡华、1 小时前
探索 Electron:将 Web 技术带入桌面应用
前端·javascript·经验分享·electron·桌面应用