在 Java 应用程序中读取 XML 文件的最佳/最简单方法是什么?

问题描述:

目前我们的 Java 应用程序使用制表符分隔的 *.cfg 文件中保存的值.我们需要更改此应用程序,使其现在使用 XML 文件.

Currently our Java application uses the values held within a tab delimited *.cfg file. We need to change this application so that it now uses an XML file.

为了从该文件中读取值,最好/最简单的库是什么?

What is the best/simplest library to use in order to read in values from this file?

当然,根据您的需要,有很多好的解决方案.如果只是配置,你应该看看 Jakarta commons-configurationcommons-digester.

There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.

您始终可以使用标准的 JDK 方法来获取文档:

You could always use the standard JDK method of getting a document :

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

[...]

File file = new File("some/path");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);