迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

如何在 Java 中读取 XML 文件

作者:迹忆客 最近更新:2023/08/14 浏览次数:

在本文中,我们将学习如何通过使用诸如 DOM ParserXML Parserjdom2dom4j 之类的某些库将 XML 文件解析为 Java 应用程序。XML 代表 Extensible Markup Language,用于在应用程序中传递数据。


样本 XML 文件

这是本文将用来读取的一个 XML 文件示例。

<?xml version="1.0"?>
<company>
    <employee id="1001">
        <firstname>Tony</firstname>
        <lastname>Black</lastname>
        <salary>100000</salary>
    </employee>
    <employee id="2001">
        <firstname>Amy</firstname>
        <lastname>Green</lastname>
        <salary>200000</salary>
    </employee>
</company>

使用 Java 中的 DocumentBuilderFactory 读取 XML 文件

我们使用 DocumentBuilder 为构建器创建一个实例,然后使用 parse() 方法解析 XML 文件。getElementsByTagName() 方法获取 XML 的每个节点,然后使用 for 循环对节点的每个子节点进行迭代。请参见下面的示例。

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;

public class SimpleTesting 
{ 
    public static void main(String[] args) throws ParserConfigurationException, SAXException 
    {
        try {
            File file = new File("company.xml");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            System.out.println("Root Element :" + document.getDocumentElement().getNodeName());
            NodeList nList = document.getElementsByTagName("employee");
            System.out.println("----------------------------");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("Employee 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("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
                }
            }
        }
        catch(IOException e) {
            System.out.println(e);
        } 
    }
}

输出:

Root Element :company
----------------------------
Current Element :employee
Employee id : 1001
First Name : Tony
Last Name : Black
Salary : 100000

Current Element :employee
Employee id : 2001
First Name : Amy
Last Name : Green
Salary : 200000

读取 Java POJO 中的 XML 文件

如果要将 XML 数据转换为 Java 兼容类型,则可以使用 Java POJO 读取数据。在这里,我们使用 ArrayList 类型的 Employee 通过 add() 方法添加每个节点,然后使用 for 循环迭代每个对象。请参见以下示例。

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;

public class Main 
{ 
    public static void main(String[] args) throws ParserConfigurationException,
    SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File("employee.xml"));
        List<Employee> employees = new ArrayList<>();
        NodeList nodeList = document.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element elem = (Element) node;
                String firstname = elem.getElementsByTagName("firstname")
                        .item(0).getChildNodes().item(0).getNodeValue();
                String lastname = elem.getElementsByTagName("lastname").item(0)
                        .getChildNodes().item(0).getNodeValue();
                Double salary = Double.parseDouble(elem.getElementsByTagName("salary")
                        .item(0).getChildNodes().item(0).getNodeValue());
                employees.add(new Employee(firstname, lastname, salary));
            }
        }
        for (Employee empl: employees)
            System.out.println(empl.toString());
    }
}
class Employee {
    private String Firstname;
    private String Lastname;
    private double salary;
    public Employee(String Firstname, String Lastname, double salary) {
        this.Firstname = Firstname;
        this.Lastname = Lastname;
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "[" + Firstname + ", " + Lastname + ", "+ salary + "]";
    }
}

输出:

[Tony, Black, 100000.0]
[Amy, Green, 200000.0]

在 Java 中使用 jdom2 读取 XML 文件

jdom2 是一个使用 Java 类提供对 DOM 解析的支持的库。我们使用了 SAXBuilder 类和 build() 方法来将数据获取到 Document 中,然后使用 getRootElement() 方法来获取元素。请参见下面的示例。

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.SAXException;

public class Main 
{ 
    public static void main(String[] args) throws ParserConfigurationException,
    SAXException, IOException {
        try {
            File inputFile = new File("/employee.xml");
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(inputFile);
            System.out.println("Root element :" + document.getRootElement().getName());
            Element classElement = document.getRootElement();

            List<Element> studentList = classElement.getChildren();
            System.out.println("----------------------------");

            for (int temp = 0; temp < studentList.size(); temp++) {
                Element student = studentList.get(temp);
                System.out.println("\nCurrent Element :" 
                        + student.getName());
                System.out.println("First Name : "
                        + student.getChild("firstname").getText());
                System.out.println("Last Name : "
                        + student.getChild("lastname").getText());
                System.out.println("Salary : "
                        + student.getChild("salary").getText());
            }
        } catch(JDOMException e) {
            e.printStackTrace();
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

输出:

Root element :company
----------------------------
Current Element :employee
First Name : Tony
Last Name : Black
Salary : 100000

Current Element :employee
First Name : Amy
Last Name : Green
Salary : 200000

在 Java 中使用 XPath 读取 XML 文件

在这里,我们使用 Java 中的 XPath 库来解析 XML 文件。XPathFactory 类用于将所有节点编译为 NodeList,然后通过 for 循环迭代每个子节点。请参见下面的示例。

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
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;

public class Main 
{ 
    public static void main(String[] args) throws ParserConfigurationException,
    SAXException, IOException {
        try {
            File inputFile = new File("/employee.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder;
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            doc.getDocumentElement().normalize();
            XPath xPath =  XPathFactory.newInstance().newXPath();
            String expression = "/company/employee";            
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
                    doc, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node nNode = nodeList.item(i);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    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("Salary : " 
                            + eElement
                            .getElementsByTagName("salary")
                            .item(0)
                            .getTextContent());
                }
            }
        } catch (ParserConfigurationException e) {
            System.out.println(e);
        } catch (SAXException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        } catch (XPathExpressionException e) {
            System.out.println(e);
        }
    }
}

输出:

Current Element :employee
First Name : Tony
Last Name : Black
Salary : 100000

Current Element :employee
First Name : Amy
Last Name : Green
Salary : 200000

在 Java 中使用 DOM4J 读取 XML 文件

Dom4j 是另一个可以解析 Java 中 XML 文件的库。SAXReader 类的 read() 方法用于将节点读入文档。请参见下面的示例。

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

public class Main 
{ 
    public static void main(String[] args) throws ParserConfigurationException,
    SAXException, IOException {
        try {
            File inputFile = new File("employee.xml");
            SAXReader reader = new SAXReader();
            Document document = reader.read( inputFile );
            System.out.println("Root element :" + document.getRootElement().getName());
            Element classElement = document.getRootElement();
            List<Node> nodes = document.selectNodes("company/employee");
            System.out.println("----------------------------");
            for (Node node : nodes) {
                System.out.println("\nCurrent Element :"
                        + node.getName());
                System.out.println("First Name : "
                        + node.selectSingleNode("firstname").getText());
                System.out.println("Last Name : "
                        + node.selectSingleNode("lastname").getText());
                System.out.println("Salary : "
                        + node.selectSingleNode("salary").getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

输出:

Root element :company
----------------------------
Current Element :employee
First Name : Tony
Last Name : Black
Salary : 100000

Current Element :employee
First Name : Amy
Last Name : Green
Salary : 200000

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

如何从 Pandas 的日期时间列中提取月份和年份

发布时间:2024/04/23 浏览次数:160 分类:Python

我们可以分别使用 dt.year()和 dt.month()方法从 Datetime 列中提取出年和蛾。我们还可以使用 pandas.DatetimeIndex.month 以及 pandas.DatetimeIndex.year 和 strftime()方法提取年份和月份。

如何获取 Pandas DataFrame 的行数

发布时间:2024/04/23 浏览次数:71 分类:Python

本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便