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

相关推荐
我命由我12345几秒前
Spring Boot - Spring Boot 集成 MyBatis 分页实现 手写 SQL 分页
java·spring boot·后端·sql·spring·java-ee·mybatis
天天摸鱼的java工程师14 分钟前
每天导入100万数据导致数据库死锁?
java·后端·面试
吗喽对你问好16 分钟前
java 知识点表格
java·开发语言
暖木生晖34 分钟前
动画序列——实现一个div转一圈的效果
前端·css·html·css3·html5
Xxtaoaooo34 分钟前
手撕Spring底层系列之:IOC、AOP
java·后端·spring·spring框架·底层源码剖析
m0_5350646036 分钟前
C++类模版与友元
java·c++·算法
山河木马36 分钟前
前端学C++可太简单了:导入标准库
前端·javascript·c++
Hilaku40 分钟前
你不会使用css函数 clamp()?那你太low了😀
前端·css·html
三小河41 分钟前
解决小项目中的频繁部署-适用于没有Jenkins或者没有配置流水线的前端部署
前端