在Java应用程序中读取XML文件的最佳/最简单方法是什么? [英] What is the best/simplest way to read in an XML file in Java application?

查看:117
本文介绍了在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);

这篇关于在Java应用程序中读取XML文件的最佳/最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆