如何在 Java 中读取 XML 文件

如何在 Java 中读取 XML 文件

样本 XML 文件

使用 Java 中的 DocumentBuilderFactory 读取 XML 文件

读取 Java POJO 中的 XML 文件

在 Java 中使用 jdom2 读取 XML 文件

在 Java 中使用 XPath 读取 XML 文件

在 Java 中使用 DOM4J 读取 XML 文件

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

样本 XML 文件

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

Tony

Black

100000

Amy

Green

200000

使用 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 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 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 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

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe


武当山特区景区街道太子坡村
闭上眼,你脑海里有一只红苹果吗? 揭秘“心盲症”:为何有些人无法在脑海中形成图像