如何使用 Java 读取 XML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2333479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read an XML file with Java?
提问by Yatendra Goel
I don't need to read complex XML files. I just want to read the following configuration file with a simplest XML reader
我不需要阅读复杂的 XML 文件。我只想用最简单的XML阅读器读取下面的配置文件
<config>
<db-host>localhost</db-host>
<db-port>3306</db-port>
<db-username>root</db-username>
<db-password>root</db-password>
<db-name>cash</db-name>
</config>
How to read the above XML file with a XML reader through Java?
如何通过Java用XML阅读器读取上述XML文件?
采纳答案by Bozho
Since you want to parse config files, I think commons-configurationwould be the best solution.
既然你想解析配置文件,我认为commons-configuration将是最好的解决方案。
Commons Configuration provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources (including XML)
Commons Configuration 提供了一个通用的配置接口,它使 Java 应用程序能够从各种来源(包括 XML)读取配置数据
回答by dariopy
There are several XML parsers for Java. One I've used and found particularly developer friendly is JDOM. And by developer friendly, I mean "java oriented" (i.e., you work with objects in your program), instead of "document oriented", as some other tools are.
有几个用于 Java 的 XML 解析器。我使用过并发现对开发人员特别友好的一个是JDOM。对于开发人员友好,我的意思是“面向 Java”(即,您在程序中使用对象),而不是“面向文档”,就像其他一些工具一样。
回答by Luhar
You could use a simple DOM parser to read the xml representation.
您可以使用一个简单的 DOM 解析器来读取 xml 表示。
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("config.xml");
回答by Llistes Sugra
I like jdom:
我喜欢 jdom:
SAXBuilder parser = new SAXBuilder();
Document docConfig = parser.build("config.xml");
Element elConfig = docConfig.getRootElement();
String host = elConfig.getChildText("host");
回答by Cshah
For a similar use case in my application I used JaxB. With Jaxb, reading XML files is like interacting with Java POJOs. But to use JAXB you need to have the xsd for this xml file. You can look for more info here
对于我的应用程序中的类似用例,我使用了 JaxB。使用 Jaxb,读取 XML 文件就像与 Java POJO 交互。但是要使用 JAXB,你需要有这个 xml 文件的 xsd。您可以在此处查找更多信息
回答by Adamski
I would recommend Commons Digester
, which allows you to parse a file without writing reams of code. It uses a series of rules to determine what action is should perform when encountering a given element or attribute (a typical rule might be to create a particular business object).
我会推荐Commons Digester
,它允许您在不编写大量代码的情况下解析文件。它使用一系列规则来确定遇到给定元素或属性时应执行的操作(典型规则可能是创建特定业务对象)。
回答by Jorn
回答by Ben Jakuben
If you just need a simple solution that's included with the Java SDK (since 5.0), check out the XPath package. I'm sure others perform better, but this was all I needed. Here's an example:
如果您只需要一个包含在 Java SDK(自 5.0 起)中的简单解决方案,请查看 XPath 包。我相信其他人的表现会更好,但这就是我所需要的。下面是一个例子:
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
...
try {
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource inputSource = new InputSource("strings.xml");
// result will equal "Save My Changes" (see XML below)
String result = xpath.evaluate("//string", inputSource);
}
catch(XPathExpressionException e) {
// do something
}
strings.xml
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="saveLabel">Save My Changes</string>
</resources>
回答by Omar Abdel Bari
Although I have not tried XPath yet as it has just come to my attention now, I have tried a few solutions and have not found anything that works for this scenario.
虽然我还没有尝试过 XPath,因为它现在才引起我的注意,但我已经尝试了一些解决方案,但没有找到任何适用于这种情况的方法。
I decided to make a library that fulfills this need as long as you are working under the assumptions mentioned in the readme. It has the advantage that it uses SAX to parse the entire file and return it to the user in the form of a map so you can lookup values as key -> value.
只要您在自述文件中提到的假设下工作,我决定制作一个满足此需求的库。它的优点是它使用 SAX 来解析整个文件并以映射的形式将其返回给用户,因此您可以将值作为键 -> 值进行查找。
https://github.com/daaso-consultancy/ConciseXMLParser
https://github.com/daaso-consultancy/ConciseXMLParser
If something is missing kindly inform me of the missing item as I only develop it based on the needs of others and myself.
如果缺少某些东西,请告知我缺少的物品,因为我仅根据他人和我自己的需要开发它。