在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?

根据您的需要,当然有很多好的解决方案。如果只是配置,你应该看看雅加达 commons-configuration commons-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);